【发布时间】:2023-03-10 10:13:01
【问题描述】:
我在名为“com.example.test”的包中编写了以下测试类。我在清单中包含以下工具:
但是当我将它作为 Android jUnit 测试运行时,我收到以下错误:
junit.framework.AssertionFailedError: Class com.example.test.TestCase 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)
你能帮帮我吗?谢谢你:)
import com.example.MenuPage;
import com.example.R;
import android.content.Intent;
import android.test.ActivityUnitTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.widget.Button;
public class TestCase extends ActivityUnitTestCase<MenuPage> {
Intent mLaunchIntent;
public TestCase(Class<MenuPage> activityClass) {
super(activityClass);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mLaunchIntent = new Intent(getInstrumentation().getTargetContext(), MenuPage.class);
startActivity(mLaunchIntent, null, null);
final Button launchNextButton = (Button) getActivity().findViewById(R.id.button1);
}
@MediumTest
public void testNextActivityWasLaunchedWithIntent() {
startActivity(mLaunchIntent, null, null);
final Button launchNextButton = (Button) getActivity().findViewById(R.id.button1);
launchNextButton.performClick();
final Intent launchIntent = getStartedActivityIntent();
assertNotNull("Intent was null", launchIntent);
assertTrue(isFinishCalled());
}
}
【问题讨论】:
-
错误消息对我来说似乎很清楚 - 它期望一个没有任何参数的公共构造函数,或者只是一个字符串参数。您希望如何将类参数提供给构造函数?
标签: java android unit-testing junit