2016-11-17 116 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中所述?