robolectric
Mock class in class under test
You could refactor MyClass so that it uses dependency injection. Instead of having it create an AnythingPerformerClass instance you could pass in an instance of the class to the constructor of MyClass like so : class MyClass { private final AnythingPerformerClass clazz; MyClass(AnythingPerformerClass clazz) { this.clazz = clazz; } public boolean performAnything() { return clazz.doSomething(); … Read more
How to run unit tests with Android Studio
just add folder named instrumentTest under /src it should have /java inside like this then extend the class ActivityTestCase (or any other android unit-test-class), such as package com.example.app.test; import android.test.ActivityTestCase; import junit.framework.Assert; public class MainActivityTest extends ActivityTestCase { public void testHappy(){ Assert.assertTrue(true); } } right click on green java directory and select run all tests … Read more
Robolectric RuntimeEnvironment.application is deprecated, where is ApplicationProvider.getApplicationContext()?
As described in the Robolectric Migration Guide to 4.0 you have to add the androidx test core dependencies testImplementation ‘androidx.test:core:1.0.0’ to your build.gradle After this you can use the ApplicationProvider.getApplicationContext() method
How to debug when running Robolectric tests in Android Studio?
I found the reason of the error. To make it run you need to uncheck the “Use in-process build” option in Compiler -> Gradle settings of Android Studio.
Robolectric – Package targetSdkVersion=30 > maxSdkVersion=29
Found this post with the same issue, with a different error message. This answer helped me to solve the issue. Posting the same here. Create a robolectric.properties file inside the app/src/test/resources directory with the following line: sdk=29 This will force Robolectric to use API 29 instead of 30. Note: Robolectric supports up to SDK 29 … Read more
Robolectric unit tests fail after Multidex
Just add multi-dex shadow dependency: testImplementation “org.robolectric:shadows-multidex:4.0.1” If you are using Robolectric < 3.4 instead use: testImplementation “org.robolectric:multidex:3.4.2”