Roboletric 4.8.2 修改进程名:
import org.robolectric.shadows.ShadowApplication; ShadowApplication.setProcessName();
Roboletric 4.8.2 修改私有静态变量:
import org.robolectric.util.ReflectionHelpers; ReflectionHelpers.setStaticField(xx.class, "static filed name", value);
如果报错:
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation. at androidx.test.platform.app.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45) at androidx.test.core.app.ApplicationProvider.getApplicationContext(ApplicationProvider.java:41)
则修改方式如下:
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*", "androidx.*"})
完整的例子如下:
import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunnerDelegate; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; /** * Base class extended by every Robolectric test in this project. * <p/> * You can use Powermock together with Robolectric. */ @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(RobolectricTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) @PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*", "androidx.*"}) public abstract class RobolectricTest { }