Unit Tests with Mockito and Jacoco in The Main Android Architectures — MVC, MVP, MVVM, VIPER, MVI, Clean— part 5: VIPER

André Figas
3 min readOct 18, 2022

--

VIPER

If you have some doubts about this architecture, check my other article.

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.

Router

This layer is a kind of spin-off of the view layer. Then that has to be tested like the view layer.

Entity
Everything we told about the model layer applies to what this architecture defines as an entity layer.

This layer is entirely testable, but It was already done when we tested an MVC project. I was following an incremental methodology. So, I won’t repeat myself. Here I will just refer to my previous article.

Presenter

This layer is completely testable, but you already saw it on the MVP article. The only difference:
- Since this architecture use a Interactor layer between model and presenter, I had to mock it.
- I have to mock and check interactions with the router layer beyond view layer.

I did not coved all known possibilities (success, fail and cancel) in this explanation to avoid repeated that o told on the MVP article.

Interactor

Success

I basically mocked a model layer that always returns the same successful result, then I checked if our code never passed through the failure callback, and If the success callback brought the expected value.

Failure

Here we validated exactly the opposite. We checked If our code did not pass through the success callback, and If our failure callback was called with our expected throwable.

Cancel

As you saw in my previous samples, when We wish validate cancelation flow, we can not move the our io thread for our synchronous flow. We really wanna It takes some time, because our plan is cancel It during the process. Then, If you compare cancelSetup with completeSetup, you will notice I removed that instruction here:

RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }

About what we validated here, basically I assure our code did not passed throuth success or failure callback. Then we checked If our release method really released some active requests.

Generating Test Results Report

./gradlew app:testViper_UnitTest

Output

app/build/reports/tests/testViper_UnitTest/index.html

Generating Coverage Report

./gradlew app:testViper_UnitTestCoverage

Output

app/build/reports/jacoco/testViper_UnitTestCoverage/index.html

--

--