2014-12-31 71 views
1

我有一個基於TeamCity的構建,它通過Gradle運行單元測試。無論如何,涉及片段或活動的測試都會失敗,ClassNotFoundException對於像android.support.v4.app.FragmentTransitionCompat21$ViewRetrieverandroid.support.v4.app.ActivityCompat21$SharedElementCallback21等類別。測試在啓動片段時失敗,並且我嘗試了所有的方法來從這個問題開始片段 - How can I test fragments with Robolectric?碎片Robolectric單元測試與ClassNotFoundException異常失敗

下面是一個測試的例子:

@Test 
public void ContactSupportFragment_CallBtnClicked_CallWasMade() throws Exception 
{ 
    ContactSupportFragment fragment = new ContactSupportFragment(); 
    startFragment(fragment); 

    LinearLayout btnCall = (LinearLayout) fragment.getView().findViewById(R.id.contact_support_call_btn); 
    btnCall.performClick(); 

    Mockito.verify(techSupportCall, Mockito.times(1)).call(Mockito.any(Context.class), 
     Mockito.eq(Robolectric.application.getString(R.string.tech_support_phone_number))); 
} 

下面是一個堆棧跟蹤的例子:

java.lang.NoClassDefFoundError: android/support/v4/app/FragmentTransitionCompat21$ViewRetriever 
at android.support.v4.app.FragmentManagerImpl.beginTransaction(FragmentManager.java:481) 
at org.robolectric.util.FragmentTestUtil.startFragment(FragmentTestUtil.java:25) 
at com.asurion.solutohome.callsupport.ContactSupportFragmentTest.ContactSupportFragment_CallBtnClicked_CallWasMade(ContactSupportFragmentTest.java:70) 
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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
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:158) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86) 
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49) 
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69) 
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:48) 
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) 
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) 
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32) 
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93) 
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source) 
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105) 
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) 
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) 
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360) 
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.ClassNotFoundException: android.support.v4.app.FragmentTransitionCompat21$ViewRetriever 
at org.robolectric.bytecode.AsmInstrumentingClassLoader.loadClass(AsmInstrumentingClassLoader.java:88) 
at android.support.v4.app.FragmentManagerImpl.$$robo$$FragmentManagerImpl_917e_beginTransaction(FragmentManager.java:481) 
at android.support.v4.app.FragmentManagerImpl.beginTransaction(FragmentManager.java) 
at org.robolectric.util.FragmentTestUtil.startFragment(FragmentTestUtil.java:25) 
at com.asurion.solutohome.callsupport.ContactSupportFragmentTest.ContactSupportFragment_CallBtnClicked_CallWasMade(ContactSupportFragmentTest.java:70) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
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:158) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86) 
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49) 
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69) 
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:48) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) 
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) 
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32) 
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93) 
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source) 
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 

它並沒有發生在當地的是,僅在TeamCity的代理,不過我還沒在我的機器和代理之間找不到任何東西(相同的SDK,相同的Gradle構建等)。 什麼會導致這些異常?

+0

如果您嘗試第二次運行而不清潔。它通過了嗎? –

+0

@EugenMartynov它通過了,但是因爲它無法互相干涉,所以我不能確定它是因爲我沒有乾淨地運行......如果它是相關的,那麼問題是什麼意思? – Fastas

+0

在Robolectic Github上有幾個問題(https://github.com/robolectric/robolectric/issues/1321)。 CI上的問題彈出窗口在沒有清理的情況下第二次運行時會生成並通過。它看起來像robolectric gradle插件的問題,混亂的任務名稱和適當的classpathes –

回答

2

因此,按照@ EugenMartynov的建議,我在構建運行測試之前向TeamCity構建添加了一個額外的組裝構建步驟,並且到目前爲止所有構建都通過了。 Robolectric似乎確實在CI構建方面存在一些問題。

因此,要總結,而不是大樓一次和運行測試,我的構建運行:

  • 乾淨,重新建造
  • 構建和運行測試

所有的測試都通過每次構建。

+0

在運行測試之前,我能夠通過gradle作爲preTest任務重新啓動組裝測試,但這並沒有爲我解決問題。 – markshiz

+0

我不得不手動運行'gradle assemble; gradle測試'爲了這個成功地運作。看起來好像某些東西在構建系統中沒有完全解決。 – markshiz

0

,我沒有太多的進入Robolectric的話題,但你送的樣子,你忘了加上二進制文件或罐子,其中包括類android.support.v4.app.FragmentTransitionCompat21堆棧跟蹤(我認爲這類包含在android SDK中)傳遞給你的類路徑。

+0

我對Gradle還不是很有經驗,但據我所知,classpaths的概念與它有點不同。該模塊已經依賴於支持庫,並且從我的理解中,將它添加到類路徑隻影響構建過程本身。 – Fastas

0

根據@Fastas的答案。您可以運行

gradle test 

這將解決問題,但至於確切原因,目前尚不清楚。

對於我自己來說,我認爲這與我的app項目中的依賴關係並不完全解決外部app-test項目的第一個構建。我在app-test/build.gradle中使用以下代碼。

dependencies { 
    // other dependencies ... 

    compile project(':app') 
    testCompile androidModule.android.applicationVariants.toList().first().javaCompile.classpath 
    testCompile androidModule.android.applicationVariants.toList().first().javaCompile.outputs.files 
    testCompile files(androidModule.plugins.findPlugin("com.android.application").getBootClasspath()) 
} 

以上,可能classpathoutputs.files並沒有完全解決,直到assemble任務完成。

更新:2015年3月23日

移動我的測試套件,解決了這個問題對我來說,應用程序模塊內的實驗測試支持。