Unit test cases using JUnit (robolectric or mockito or both in Android)
This is the first time I have written unit test cases in Android
So I searched a lot of things
>Robolectric framewordk – run on JVM > mockito framework – imitate objects
So I'm at robolectric & mokito
>Should I only use JUnit's robolectric in Android applications? > Should I only use mockito and JUnit in Android applications? > Should I use two frames? > Mockito& Robolectric?
I was looking for mokito & robolectric, but I didn't get any appropriate answer
Please advise
Solution
Their use is slightly different, and I prefer to use it in my project
Mockito
Used to make mockery of your course
When you test a specific class, you can use mockito to simulate all dependencies
If possible, most tests should use mockito In order to make this possible, most people decompose their code into MVP, in which business logic is separated from view logic In this way, your presenter has no knowledge (or dependencies) on the Android library, and you don't need to laugh at them
Robolectric
Is a simulation library containing many Android classes
When the test runs, robolectric test runner will inject these "shadow objects" to replace the actual Android class This allows the test to run on the JVM without starting the Android instance
When using MVP, your view layer is often implemented by activity / fragment. You can simulate these using robolectric
note
Use robolectric only if necessary It basically re - implements parts of the Android framework, but not always in exactly the same way
You may also need another library, such as powermock This allows you to mock static classes, such as math, or Android classes that can be used to simulate static, such as textutils
Both are used with JUnit