5

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

   ./gradlew clean :app:connectedDebugAndroidTest

spring 测试对 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:找不到类: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:找不到类: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:找不到类: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:找不到类: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:找不到类: 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:找不到类: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。网。MockRequestDispatcher 08-05 00:04:12.623 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类: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:找不到类: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 在 java.lang.Class。classForName(Native Method) at java.lang.Class.forName(Class.java:324) at android.support.test.internal.runner.TestLoader.doLoadClass(TestLoader.java:92) at android.support.test.internal。 runner.TestLoader.loadIfTest(TestLoader.java:113) at android.support.test.internal.runner.TestRequestBuilder.loadClassesFromClassPath(TestRequestBuilder.java:801) at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.爪哇:747) 在 android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:354) 在 android.app.Instrumentation$InstrumentationThread.run (Instrumentation.java:1879)java:260) 在 android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)java:260) 在 android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)

4

1 回答 1

1

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

您可能正在使用一些不幸的 jar 构建,其中模拟类的依赖项丢失了,就像在这个问题中一样。

你也可以尝试更新spring框架,最新的已经是4.3.2了。

于 2016-08-12T09:33:52.147 回答