Unit Tests with Mockito and Jacoco in The Main Android Architectures — MVC, MVP, MVVM, Clean, MVI, VIPER, VIPER — part 2: MVC
MVC
If you have some doubts about this architecture, check my other article.
Controller
For sure this is not my favorite architecture. At least not as that was applied for Android Development. Among other reasons, our Activity works as a controller, It makes a connection with our model layer, even if it is not testable.
I can prove my point, through two arguments:
- Our unit tests should not depend on some Android library or another kind of visual component. We have Instrumented Test to handle this kind of situation. If this is your wish, I suggest reading about Espresso.
- Activities are not created by constructors, so we could not send for it any kind of mock component.
View
The view layer definitely is not the proposal for this kind of test. That assertion is valid for all architectures. I would recommend you again read about Espresso, but since the MVC understands static .xml files as the view layer, this makes impossible any kind of tests through this layer.
So, remains for us just test the model layer.
Model
Implementation
If you are following me since the last sequence of articles, maybe you will notice I am using now RxJava instead of AsyncTask.
I have tried to make it as simple as possible, and here I decided to use RxJava to make easy my tests, and, additionally, I guess this makes it easy for you to adapt it for some viable alternative for handling asynchronous access to resources from some API. If this topic is relevant to you, check this article.
Tests
Usually, in our API Rest, We want to move a task for background, make the main thread free for the user to keep interacting with our app, and when we have feedback, return something for our main thread. But here, We want to handle it like a blocking-queueing as if all of this be happen in the same thread. Because of this, RxJava requires this setup implementation to be called before our tests.
Now, let's see which report can this implementation produce.
Generating Test Results Report
./gradlew app:testMvc_UnitTest
Output
app/build/reports/tests/testMvc_UnitTest/index.html
Generating Coverage Report
./gradlew app:testMvc_UnitTestCoverage
Output
app/build/reports/jacoco/testMvc_UnitTestCoverage/index.html
You can check this project on GitHub. Remember to select the architecture that you wish to study on the build variants:
Unit Tests with Mockito and Jacoco in The Main Android Architectures — part 2: MVC
Unit Tests with Mockito and Jacoco in The Main Android Architectures — part 3: MVP
Unit Tests with Mockito and Jacoco in The Main Android Architectures — part 4: MVVM
Unit Tests with Mockito and Jacoco in The Main Android Architectures — part 5: VIPER
Unit Tests with Mockito and Jacoco in The Main Android Architectures — part 6: MVI
Unit Tests with Mockito and Jacoco in The Main Android Architectures — part 7: Clean