2014-08-27 110 views
4

我想运行与Espresso特别是双浓咖啡库的Android仪器测试。安卓咖啡与匕首

我知道Espresso已经对匕首1.2.1有依赖性,我的应用也使用相同的匕首版本。 所以我宣布这样的依赖。

// dagger-compiler already includes dagger. 
compile 'com.squareup.dagger:dagger-compiler:1.2.1' 

androidTestCompile 'com.google.guava:guava:16.0' 
androidTestCompile 'javax.annotation:javax.annotation-api:1.2' 
androidTestCompile 'com.google.code.findbugs:jsr305:1.3.9' 
androidTestCompile('com.jakewharton.espresso:espresso-support-v4:1.1-r3') { 
    exclude group: 'com.android.support', module: 'support-v4' 
    exclude group: 'com.google.guava', module: 'guava' 
    exclude group: 'com.squareup.dagger' 
    //exclude group: 'javax.annotation', module: 'javax.annotation-api' 
} 

而我的测试看起来像这样。

@LargeTest 
public class IntegrationTest extends ActivityInstrumentationTestCase2<LoginSelectMethodActivity> { 

    public IntegrationTest() { 
    // This constructor was deprecated - but we want to support lower API levels. 
     super("com.google.android.apps.common.testing.ui.testapp", LoginSelectMethodActivity.class); 
    } 
    @Override 
    public void setUp() throws Exception { 
     super.setUp(); 
     getActivity(); 
    } 

    @LargeTest 
    public void test_actionButtonEnableWhenInputsAreValid() { 
     Espresso.onView(ViewMatchers.withId(R.id.signin_email)) 
       .perform(ViewActions.click()); 

     Espresso.onView(ViewMatchers.withText(R.id.et_email)) 
       .perform(ViewActions.typeText("[email protected]"), ViewActions.pressKey(KeyEvent.KEYCODE_ENTER), 
         ViewActions.typeText("aGoodP455w0rd"), ViewActions.pressKey(KeyEvent.KEYCODE_ENTER)); 

     Espresso.onView(ViewMatchers.withId(R.id.b_signin)) 
       .check(matches(withText(R.string.sign_in))); 
    } 

} 

但是,测试失败,日志说它找不到一个匕首的方法。

08-27 22:24:13.979 4074-4087/com.trumpia.android.loyaltea.debug W/dalvikvm﹕ VFY: unable to resolve virtual method 15010: Ldagger/ObjectGraph;.get (Ljava/lang/Class;)Ljava/lang/Object; 
08-27 22:24:13.979 4074-4087/com.trumpia.android.loyaltea.debug D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0015 
08-27 22:24:13.979 4074-4087/com.trumpia.android.loyaltea.debug I/dalvikvm﹕ Could not find method dagger.ObjectGraph.get, referenced from method com.google.android.apps.common.testing.ui.espresso.Espresso.registerIdlingResources 
08-27 22:24:13.979 4074-4087/com.trumpia.android.loyaltea.debug W/dalvikvm﹕ VFY: unable to resolve virtual method 15010: Ldagger/ObjectGraph;.get (Ljava/lang/Class;)Ljava/lang/Object; 
08-27 22:24:13.979 4074-4087/com.trumpia.android.loyaltea.debug D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0009 
08-27 22:24:13.979 4074-4087/com.trumpia.android.loyaltea.debug I/dalvikvm﹕ Could not find method dagger.ObjectGraph.get, referenced from method com.google.android.apps.common.testing.ui.espresso.Espresso.registerLooperAsIdlingResource 
08-27 22:24:13.979 4074-4087/com.trumpia.android.loyaltea.debug W/dalvikvm﹕ VFY: unable to resolve virtual method 15010: Ldagger/ObjectGraph;.get (Ljava/lang/Class;)Ljava/lang/Object; 
08-27 22:24:13.979 4074-4087/com.trumpia.android.loyaltea.debug D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0006 
08-27 22:24:13.979 4074-4087/com.trumpia.android.loyaltea.debug I/dalvikvm﹕ Could not find method dagger.ObjectGraph.get, referenced from method com.google.android.apps.common.testing.ui.espresso.Espresso.setFailureHandler 
08-27 22:24:13.979 4074-4087/com.trumpia.android.loyaltea.debug W/dalvikvm﹕ VFY: unable to resolve virtual method 15010: Ldagger/ObjectGraph;.get (Ljava/lang/Class;)Ljava/lang/Object; 
08-27 22:24:13.979 4074-4087/com.trumpia.android.loyaltea.debug D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0006 
... 
08-27 22:24:14.031 4074-4087/com.trumpia.android.loyaltea.debug I/TestRunner﹕ ----- begin exception ----- 
08-27 22:24:14.031 4074-4087/com.trumpia.android.loyaltea.debug I/TestRunner﹕ java.lang.NoSuchMethodError: dagger.ObjectGraph.get 
      at com.google.android.apps.common.testing.ui.espresso.Espresso.onView(Espresso.java:51) 

我运行Android Studio中的试验与GoogleInstrumentationTestRunner。 希望有人能为我提供一点点点亮。

回答

4

你需要依靠匕首。

替换此:

// dagger-compiler already includes dagger. 
compile 'com.squareup.dagger:dagger-compiler:1.2.1' 

与此:

compile 'com.squareup.dagger:dagger:1.2.1' 
provided 'com.squareup.dagger:dagger-compiler:1.2.1' 
+0

哦,太棒了...其实一开始我得到了,但是我发现,我打开'runProguard TRUE'在'调试同样的错误'模式。所以,如果我关闭它的作品。我打开它,因为如果我关掉它给了我错误'方法ID不在[0,0xffff]:65536'。通过将dagger-compiler标记为“提供”的代码,我可以避免该错误。谢谢! – Chk0nDanger 2014-08-28 10:55:46