Testing in Android. Unit test check list
Tags: java, android, testing, tristan
Published: 2021-10-30 17:44:58

###Introduction
- Now that I have officially released my first app, which can be found [HERE](https://play.google.com/store/apps/details?id=com.elliottSoftware.ecalvingtracker&hl=en&gl=US) on the Google Play store. I want to add more features to my app, however, I think the next best step is to add some tests. So, this series is going to be a practical series dedicated to understanding testing in the Android framework.

###The checklist
- As I have finished my instrumented tests for now, I have moved on to unit testing my code. Below is a checklist that I have developed for unit testing. This is an example from testing my repository layer but it can be applied to other features/layers as well.

**1) Identify the dependencies :** This is the first and arguably the most important when unit testing.

![shows the dependency injection pattern](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kgr0mms72ub2j60juw84.png)
 
- If you look at the image above you will notice the red circle where I have identified my dependency for this class. Also, notice I am implementing the [dependency injection](https://developer.android.com/training/dependency-injection) pattern. This will allow us to easily test the repository class later on.

**2) Set up the tests :** 
![initial test setup with mockito](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lzul5xcpy8s6o90zas2k.png)

- This step is a rather large one but it involves us first creating a new class in the unit testing folder. Then we identify what is being tested, `private CalfRepository calfRepository;`. next we identify what the dependencies are, `private CalfDao calfDao;`. Then we use the `@Before` annotation to set up a method to run before every test. Then we use [Mockito](https://site.mockito.org/) to set up a mocked object of the dependencies, `Mockito.mock(CalfDao.class);`. We use mocked objects because they allow us to test our repository layer in isolation. With a mocked object we can control with 100% certainty what each method will return. The last part in this section is to create a new instance of whatever we are testing, for this tutorial it is, `calfRepository = new CalfRepository(calfDao);`.

**2) Implement the tests**
![Test example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wbv5eogj2fpwvfod8g6.png)

- Of course the logic you use for your tests will change. However, I marked what should stay the same with red Xs. In order for us to create a test, we have to mark a method with the `@Test` annotation. We also mock the methods that are being called in our dependencies, this way they always return what we want them to. The reliable return values allows us to test the repository layer in isolation. The mocked method is done by, `Mockito.when(calfDao.properInsert(Mockito.any(Calf.class))).thenReturn(returnedData);`.


**3) Rinse and repeat :** Testing a very repetitive, so once you have done it for one method, do it for the next method and so on.


#Conclusion
- Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on [Twitter](https://twitter.com/TristAndroidDev).