MockWebServer抛出java.lang.reflect.InvocationTargetException

时间:2017-02-07 15:01:43

标签: java android unit-testing retrofit2 mockwebserver

我在这里关注代码:Android Unit Test with Retrofit2 and Mockito or Robolectric

我正在尝试运行此代码:

mockWebServer.enqueue(new MockResponse().setBody("your json body"));

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(mockWebServer.url("").toString())
                //TODO Add your Retrofit parameters here
                .build();

但是在build()方法中抛出:

java.lang.reflect.InvocationTargetException 


java.lang.AssertionError
    at okhttp3.internal.AndroidPlatform.isCleartextTrafficPermitted(AndroidPlatform.java:143)
    at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:73)
    at retrofit2.Retrofit$Builder.build(Retrofit.java:551)
    at com.test.mc.ia.UnitTests.testGetUsers(UnitTests.java:86)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:498)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:245)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:171)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:137)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)

我无法弄明白为什么

1 个答案:

答案 0 :(得分:0)

啊想通了。这是Robolectric:

改变:

@Config(constants = BuildConfig.class,sdk = JELLYBEAN,packageName =&#34; com.test.mc.instagram&#34;)

@Config(constants = BuildConfig.class,sdk = 23,packageName =&#34; com.test.mc.instagram&#34;)

看到这个: https://github.com/square/okhttp/issues/2591