Join Chiu-Ki Chan for an in-depth discussion in this video UI-less on-device test: Null test, part of Effective Android Testing for Mobile Developers.
- [Instructor] To test recipe store dot Java, go to the file and then put your cursor in the name of the class and press command shift T to generate a new test. Press okay, and choose Android test because we will need to use the Android context in this test. Press okay. First, let's test the null case. Press command N to generate a test method.
Choose J Unit four and then we will call it null directory. Remove the throws Exception line and then first, we need a context so that we can pass it to the recipe store. Context context equal instrumentation registry and then you do dot. You can see that there are two functions, both returning context. One is get context, and the other one is get target context.
Make sure you choose get target context which returns the context for the app. Get context, on the other hand, returns the context for the test. We will use the context from the app because we are going to be testing the recipes that is bundled within the app. Create a recipe store with this context and null as the directory. Recipe store, we'll call it store, equal new recipe store, context comma null.
What is the expected behavior of this store? We want it to return a store that is not null and contains no recipe. So first, we will assert that it is not null. Assert not null, store. Run the test by clicking on the green triangle on the gutter. This time, unlike the recipe test, we are presented the emulator dialogue. This is because we are using the Android instrumentation test which requires either a device or an emulator to run.
Choose your emulator and click on use same selection for future launches. Press okay. This will launch the Android emulator. Yay, all tests passed. Notice that we started the emulator, but the UI didn't change. This is why this is called UILS on device test or UILS instrumentation test. We need the emulator so that we can use the Android classes when we are not actually verifying anything on the UI.
Let's continue with this test. Go to line sixteen and we are going to assert that this recipe store is empty. Assert equals zero comma store dot recipes dot size. This is why earlier we are putting the recipes into a public field. Let's go to recipe store just to look at it. In line fifteen, you can see that it is a public final field, recipes.
This way, after we have passed all the recipes out, we can actually use it. Go back to the test, and now we know that we have these recipes, we want to verify that it is empty, meaning that the size is zero. Before we do that, let's add a line before it to verify that it is not null. Assert not null, store dot recipes. This way, we will have two tests.
One, the recipes are not null. And then it is empty. Press the green triangle to run the test again. Again, all tests passed.
- Why test?
- Local vs. on-device
- Code coverage
- UI testing
- Hermetic environment
- Dependency injection
- Testing with MVP