Unit tests are difficult or impossible when using Realm in the class you are testing (thanks to Dmitry for mentioning). What I can do is run the tests as instrumental tests (thanks Dmitry, Christian).
And it's pretty simple, I donโt need to change anything for the testing methods ...
a. Move the test class to the "androidTest" folder instead of "test". (Starting with Android Studio 1.1, you should put your unit tests in / src / test and Android Instrumentation Tests in / src / androidTest)
B. Add dependencies for instrumental tests in the gradle assembly file, use "androidTest" because they are instrumental:
androidTestCompile 'junit:junit:4.12' androidTestCompile 'io.reactivex:rxjava:1.1.0' androidTestCompile 'com.android.support.test:runner:0.4.1' androidTestCompile 'com.android.support.test:rules:0.4.1' androidTestCompile 'com.android.support:support-annotations:23.1.1'
C. In the test class, replace the runner at the top with AndroidJUnit4:
@RunWith(AndroidJUnit4.class) public class MyClassTest extends TestCase { ...
Create an Android launch configuration like โAndroid Testsโ, launch it and voila, it will check the same methods now, but on the device. I am very happy.
Frank source share