Error“在Junit测试中没有公共TestCase(String name)或TestCase() [英] Error "has no public TestCase(String name) or TestCase() in Junit test
本文介绍了Error“在Junit测试中没有公共TestCase(String name)或TestCase()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是junit test android的初学者。我正在关注本教程,但收到此错误
I'm a beginner in junit test android. I'm following this tutorial but get this error
junit.framework.AssertionFailedError: Class com.example.projectfortest.test.MainActivityTest has no public constructor TestCase(String name) or TestCase()
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
这是我的代码:
public class MainActivityTest extends
ActivityInstrumentationTestCase2<MainActivity> {
private MainActivity mMainActivity;
private TextView mFirstTestText;
private String expected, actual;
public MainActivityTest(Class<MainActivity> activityClass) {
super(activityClass);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mMainActivity = getActivity();
mFirstTestText = (TextView) mMainActivity.findViewById(R.id.txt1);
expected = mMainActivity.getString(R.string.txt1_test);
actual = mFirstTestText.getText().toString();
}
public void testPreconditions() {
assertNotNull("mMainActivity is null", mMainActivity);
assertNotNull("mFirstTestText is null", mFirstTestText);
}
public void testMyFirstTestTextView_labelText() {
assertEquals(expected, actual);
}
}
我的代码中没有看到任何错误。请帮助
I didn't see anything wrong in my code. Please help
推荐答案
正如Exception所说,为您的类添加一个默认的构造函数。这是测试框架初始化所必需的。替换构造函数:
As the Exception says add a default Constructor to your Class. This is needed for the initialisation by the testing framework. Replace your constructor:
public MainActivityTest(Class<MainActivity> activityClass) {
super(activityClass);
}
以下内容:
public MainActivityTest() {
super(MainActivity.class);
}
此构造函数没有框架所需的参数并显示在代码清单中您的教程:
This constructor has no arguments as needed by the framework and showing in the code listing of your tutorial:
这篇关于Error“在Junit测试中没有公共TestCase(String name)或TestCase()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文