我是junit test android的初学者。我正在关注this tutorial,但收到此错误
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);
}
}
我的代码中没有看到任何错误。请帮忙
答案 0 :(得分:34)
正如Exception所说,为你的类添加一个默认的构造函数。这是测试框架初始化所必需的。替换您的构造函数:
public MainActivityTest(Class<MainActivity> activityClass) {
super(activityClass);
}
通过以下内容:
public MainActivityTest() {
super(MainActivity.class);
}
此构造函数没有框架所需的参数,并显示在your tutorial的代码清单中:
答案 1 :(得分:1)
如果您的问题仍未解决: 您可以通过添加
来尝试此注释 @RunWith (JUnit4.class)