3

I'm trying to use Powermock to mock static methods on Android Espresso Tests Cases, but I'm receiving this error.

My tests are running fine with AndroidJUnit4.class Runner and I am able compile them with the PowerMockRunner, this error happens in runtime only. My app is a multidex one.

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(AndroidJUnit4.class)
@PrepareForTest(RestClientFactory.class)
public class IgorLoginGoogleDemaisTest extends ActivityInstrumentationTestCase2<MainCacs>
{
...
    @Test
    public void test1_ErroDeConexaoAoLogar() throws NoSuchFieldException, IllegalAccessException
    {
        ....
        PowerMockito.mockStatic(RestClientFactory.class);
        PowerMockito.when(RestClientFactory.makeClient(UsuarioServicesClient.class, Mockito.any(Context.class))).thenThrow(new IOException("Erro de rede"));
        ....
    }
}

This is the error I'm receiving (it's basically a kind of ClassNotFoundError for dynamic class loading):

java.lang.TypeNotPresentException: Type org/powermock/modules/junit4/PowerMockRunner not present
at java.lang.Class.getDeclaredAnnotation(Native Method)
at java.lang.Class.getAnnotation(Class.java:290)
at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.runnerForClass(AndroidAnnotatedBuilder.java:42)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runner.Computer.getRunner(Computer.java:40)
at org.junit.runner.Computer$1.runnerForClass(Computer.java:31)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
at org.junit.runners.Suite.<init>(Suite.java:81)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at android.support.test.internal.runner.TestRequestBuilder.classes(TestRequestBuilder.java:791)
at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:754)
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:341)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:238)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)

Below are my dependencies for AndroidTest scope, into build.gradle

    androidTestCompile 'org.powermock:powermock-api-mockito:1.6.2'
    androidTestCompile 'org.powermock:powermock-module-junit4-rule-agent:1.6.2'
    androidTestCompile 'org.powermock:powermock-module-junit4-rule:1.6.2'
    androidTestCompile 'org.powermock:powermock-module-junit4:1.6.2'

    androidTestCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support.test:runner:0.4'
    // Set this dependency to use JUnit 4 rules
    androidTestCompile 'com.android.support.test:rules:0.4'
    // Set this dependency to build and run Espresso tests
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'

    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'

    androidTestCompile('com.android.support:multidex-instrumentation:1.0.1') {
        exclude group: 'com.android.support', module: 'multidex'
    }

I tried to put the class name into my multidex.keep file, but it didin't work

How cound I fix it?

Igor Regis
  • 315
  • 1
  • 3
  • 8

0 Answers0