5

我正在使用安卓彈簧RestTemplate在我的應用上進行REST服務調用。我添加了Android Instrumentation測試,其中包括模擬REST服務調用。 我所有的測試均運行良好,從Android Studio中運行時,但試驗未能加載彈簧模擬類使用AndroidTest從命令行運行失敗:app:connectedDebugAndroidTest

./gradlew clean :app:connectedDebugAndroidTest 

上的build.gradle Spring的測試依賴從終端運行而

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: Could not find class: org.springframework.asm.commons.JSRInlinerAdapter 08-05 00:04:12.587 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.asm.commons.TryCatchBlockSorter 08-05 00:04:12.593 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.cglib.transform.AbstractProcessTask 08-05 00:04:12.593 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.cglib.transform.AbstractTransformTask 08-05 00:04:12.603 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.core.convert.support.ConvertingPropertyEditorAdapter 08-05 00:04:12.610 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.core.io.ResourceEditor 08-05 00:04:12.611 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.core.io.support.ResourceArrayPropertyEditor 08-05 00:04:12.617 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.http.MockHttpInputMessage 08-05 00:04:12.618 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.http.MockHttpOutputMessage 08-05 00:04:12.618 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.http.client.MockClientHttpRequest 08-05 00:04:12.618 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.http.client.MockClientHttpResponse 08-05 00:04:12.618 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.jndi.ExpectedLookupTemplate 08-05 00:04:12.619 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.jndi.SimpleNamingContext 08-05 00:04:12.619 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.jndi.SimpleNamingContextBuilder 08-05 00:04:12.619 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.DelegatingServletInputStream 08-05 00:04:12.619 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.DelegatingServletOutputStream 08-05 00:04:12.620 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.MockBodyContent 08-05 00:04:12.620 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.MockExpressionEvaluator 08-05 00:04:12.620 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.MockFilterChain 08-05 00:04:12.620 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.MockFilterConfig 08-05 00:04:12.620 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.MockHttpServletRequest 08-05 00:04:12.621 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.MockHttpServletResponse 08-05 00:04:12.621 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.MockHttpSession 08-05 00:04:12.621 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.MockJspWriter 08-05 00:04:12.621 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.MockMultipartFile 08-05 00:04:12.621 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.MockMultipartHttpServletRequest 08-05 00:04:12.622 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.MockPageContext 08-05 00:04:12.622 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.MockRequestDispatcher 08-05 00:04:12.623 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.MockServletConfig 08-05 00:04:12.623 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.MockServletContext 08-05 00:04:12.628 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.PassThroughFilterChain 08-05 00:04:12.628 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.portlet.MockActionRequest 08-05 00:04:12.629 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.portlet.MockActionResponse 08-05 00:04:12.629 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.portlet.MockBaseURL 08-05 00:04:12.629 22274-22302/com.libin.androiduitesting E/TestLoader: Could not find class: org.springframework.mock.web.portlet.MockCacheControl 08-05 00:04:12.630 22274-22302/com.libin.androiduitesting E/AndroidRuntime: FATAL EXCEPTION: Instr: android.support.test.runner.AndroidJUnitRunner Process: 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) 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.java:747) at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:354) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:260) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)

+0

'testCompile'呢? –

+0

如@ cricket_007所示,將'androidTestCompile'更改爲'testCompile'。它工作嗎? – piotrek1543

+0

不可以。編號是如何工作的,添加到testCompile的依賴項僅包含在單元測試中。我正在運行androidTest。它不包括androidTest – Libin

回答

1

好像你缺少嘲諷這是neccesary測試類。嘗試下載彈簧測試罐並檢查是否所有模擬類都在裏面 - 例如,它應該從包org.springframework.mock.web.portlet中導出類org.springframework.mock.web.portlet.MockClientDataRequest

你可以使用一些不幸的jar版本,其中嘲諷類的缺陷丟失,如this issue

你也可以嘗試更新spring框架,最新的已經是4.3.2。

+0

不,我不錯過課程。在開始運行時,要對整個模塊進行測試。如果測試是針對個人課程或課程包進行的,則一切都很好 – Libin