2016-11-17 140 views
0

我使用Espresso到運行Android的UI測試,包括所有需要的依賴後,並創建所需的類測試,運行它給我的錯誤時:無法運行測試咖啡

java.lang.Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass) 

和確定它是一個內置的類,所以我不必修改它的能力,即使我試圖創建一個自定義的類,但是這導致了我不能修復的其他問題,除非刪除我的自定義類中添加的額外構造函數,這導致我結束了同樣的問題。

我的測試類:

@RunWith(AndroidJUnit4.class) 
public class CreateTest { 

    @Rule 
    public ActivityTestRule mainActivityTest = new ActivityTestRule<>(MainActivity.class); 

    @Before 
    public void setUp() throws Exception { 
     Context context = InstrumentationRegistry.getContext(); 
    } 

    @Test 
    public void clickAdd(){ 
     Espresso.onView(withId(R.id.button)) 
       .perform(click()); 
     intended(hasComponent(new ComponentName(getTargetContext(), SecondActivity.class))); 
    } 

} 

依賴關係:

compile 'junit:junit:4.12' 

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
}) 
androidTestCompile ('com.android.support.test:runner:0.5', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
}) 
androidTestCompile ('com.android.support.test:rules:0.5', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
}) 
compile('com.android.support.test.espresso:espresso-intents:2.2.1', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
}) 

PS我很願意使用其他測試API,如果有一個可用,但我搜索,沒有發現任何東西。

+1

'@RunWith(JUnit4.class)'而不是'@RunWith(AndroidJUnit4.class)' – denys

+0

它給了我'java.lang.IllegalStateException:沒有檢測儀器註冊!必須在註冊儀器下運行。「當這樣做時,對這種錯誤的種類有什麼想法? –

+0

你可以試試谷歌搜索?有很多線程都有相同的問題。 – denys

回答

0

您是否在AndroidManifest.xml文件中聲明瞭儀器?

<instrumentation 
    android:name="android.support.test.runner.AndroidJUnitRunner" 
    android:targetPackage="com.your.packageundertest" /> 
+0

我添加了它,但也得到了與unRegistered Instrumentation相同的錯誤 –

-1

您是否已將您的儀器運行程序添加到build.gradle文件中,如設置說明here中所述?