AndroidTest从命令行运行时失败:app:connectedDebugAndroidTest

时间:2016-08-05 04:47:30

标签: android android-espresso spring-test android-instrumentation spring-android

我使用android spring RestTemplate在我的应用上进行REST服务调用。我添加了android Instrumentation测试,其中包括模拟REST服务调用。 从android studio运行时,我的所有测试运行正常,但测试无法在使用终端运行时加载spring mock类

   ./gradlew clean :app:connectedDebugAndroidTest

对build.gradle的弹簧测试依赖

    androidTestCompile("org.springframework:spring-test:3.2.8.RELEASE")

如果我改为compile而不是androidTestCompile,那么android测试从终端运行正常。 由于我不希望这种依赖于我的制作APK任何帮助,赞赏。

开始测试时的日志

  

08-05 00:04:12.585 22274-22302 / com.libin.androiduitesting   E / TestLoader:找不到类:   org.springframework.asm.commons.JSRInlinerAdapter 08-05 00:04:12.587   22274-22302 / com.libin.androiduitesting E / TestLoader:找不到   class:org.springframework.asm.commons.TryCatchBlockSorter 08-05   00:04:12.593 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到课程:   org.springframework.cglib.transform.AbstractProcessTask 08-05   00:04:12.593 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到课程:   org.springframework.cglib.transform.AbstractTransformTask 08-05   00:04:12.603 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到课程:   org.springframework.core.convert.support.ConvertingPropertyEditorAdapter   08-05 00:04:12.610 22274-22302 / com.libin.androiduitesting   E / TestLoader:找不到类:   org.springframework.core.io.ResourceEditor 08-05 00:04:12.611   22274-22302 / com.libin.androiduitesting E / TestLoader:找不到   class:org.springframework.core.io.support.ResourceArrayPropertyEditor   08-05 00:04:12.617 22274-22302 / com.libin.androiduitesting   E / TestLoader:找不到类:   org.springframework.mock.http.MockHttpInputMessage 08-05 00:04:12.618   22274-22302 / com.libin.androiduitesting E / TestLoader:找不到   class:org.springframework.mock.http.MockHttpOutputMessage 08-05   00:04:12.618 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到课程:   org.springframework.mock.http.client.MockClientHttpRequest 08-05   00:04:12.618 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到课程:   org.springframework.mock.http.client.MockClientHttpResponse 08-05   00:04:12.618 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到课程:   org.springframework.mock.jndi.ExpectedLookupTemplate 08-05   00:04:12.619 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到课程:   org.springframework.mock.jndi.SimpleNamingContext 08-05 00:04:12.619   22274-22302 / com.libin.androiduitesting E / TestLoader:找不到   class:org.springframework.mock.jndi.SimpleNamingContextBuilder 08-05   00:04:12.619 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到课程:   org.springframework.mock.web.DelegatingServletInputStream 08-05   00:04:12.619 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到课程:   org.springframework.mock.web.DelegatingServletOutputStream 08-05   00:04:12.620 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到类:org.springframework.mock.web.MockBodyContent   08-05 00:04:12.620 22274-22302 / com.libin.androiduitesting   E / TestLoader:找不到类:   org.springframework.mock.web.MockExpressionEvaluator 08-05   00:04:12.620 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到类:org.springframework.mock.web.MockFilterChain   08-05 00:04:12.620 22274-22302 / com.libin.androiduitesting   E / TestLoader:找不到类:   org.springframework.mock.web.MockFilterConfig 08-05 00:04:12.620   22274-22302 / com.libin.androiduitesting E / TestLoader:找不到   class:org.springframework.mock.web.MockHttpServletRequest 08-05   00:04:12.621 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到课程:   org.springframework.mock.web.MockHttpServletResponse 08-05   00:04:12.621 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到类:org.springframework.mock.web.MockHttpSession   08-05 00:04:12.621 22274-22302 / com.libin.androiduitesting   E / TestLoader:找不到类:   org.springframework.mock.web.MockJspWriter 08-05 00:04:12.621   22274-22302 / com.libin.androiduitesting E / TestLoader:找不到   class:org.springframework.mock.web.MockMultipartFile 08-05   00:04:12.621 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到课程:   org.springframework.mock.web.MockMultipartHttpServletRequest 08-05   00:04:12.622 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到类:org.springframework.mock.web.MockPageContext   08-05 00:04:12.622 22274-22302 / com.libin.androiduitesting   E / TestLoader:找不到类:   org.springframework.mock.web.MockRequestDispatcher 08-05 00:04:12.623   22274-22302 / com.libin.androiduitesting E / TestLoader:找不到   class:org.springframework.mock.web.MockServletConfig 08-05   00:04:12.623 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到类:org.springframework.mock.web.MockServletContext   08-05 00:04:12.628 22274-22302 / com.libin.androiduitesting   E / TestLoader:找不到类:   org.springframework.mock.web.PassThroughFilterChain 08-05 00:04:12.628   22274-22302 / com.libin.androiduitesting E / TestLoader:找不到   class:org.springframework.mock.web.portlet.MockActionRequest 08-05   00:04:12.629 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到课程:   org.springframework.mock.web.portlet.MockActionResponse 08-05   00:04:12.629 22274-22302 / com.libin.androiduitesting E / TestLoader:   找不到类:org.springframework.mock.web.portlet.MockBaseURL   08-05 00:04:12.629 22274-22302 / com.libin.androiduitesting   E / TestLoader:找不到类:   org.springframework.mock.web.portlet.MockCacheControl 08-05   00:04:12.630 22274-22302 / com.libin.androiduitesting E / AndroidRuntime:   致命异常:Instr:android.support.test.runner.AndroidJUnitRunner                                                                               过程:com.libin.androiduitesting,PID:22274                                                                               java.lang.NoClassDefFoundError:   org.springframework.mock.web.portlet.MockClientDataRequest                                                                                   at java.lang.Class.classForName(Native Method)                                                                                   at java.lang.Class.forName(Class.java:324)                                                                                   在   android.support.test.internal.runner.TestLoader.doLoadClass(TestLoader.java:92)                                                                                   在   android.support.test.internal.runner.TestLoader.loadIfTest(TestLoader.java:113)                                                                                   在   android.support.test.internal.runner.TestRequestBuilder.loadClassesFromClassPath(TestRequestBuilder.java:801)                                                                                   在   android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:747)                                                                                   在   android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:354)                                                                                   在   android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:260)                                                                                   在   android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1879)

1 个答案:

答案 0 :(得分:1)

好像你缺少了需要测试的模拟类。尝试下载spring-test jar并检查是否所有模拟类都在里面 - 例如,它应该从包org.springframework.mock.web.portlet.MockClientDataRequest导出类org.springframework.mock.web.portlet

您可以使用一些不幸的jar构建,其中模拟类的依赖性丢失,如this issue中所示。

您也可以尝试更新spring框架,最新版本已经是4.3.2。