gpt4 book ai didi

java - 错误“在 Junit 测试中没有公共(public) TestCase(字符串名称)或 TestCase()

转载 作者:太空狗 更新时间:2023-10-29 23:01:21 26 4
gpt4 key购买 nike

我是 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);
}
}

我没有发现我的代码有任何问题。请帮忙

最佳答案

正如异常所说,将默认构造函数添加到您的类中。这是测试框架初始化所必需的。替换您的构造函数:

public MainActivityTest(Class<MainActivity> activityClass) {
super(activityClass);
}

通过以下方式:

public MainActivityTest() {
super(MainActivity.class);
}

此构造函数没有框架所需的参数,并显示在 your tutorial 的代码 list 中:

关于java - 错误“在 Junit 测试中没有公共(public) TestCase(字符串名称)或 TestCase(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23881206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com