【问题标题】:Android jUnit test errorAndroid jUnit测试错误
【发布时间】: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


【解决方案1】:

junit.framework.AssertionFailedError: 类 com.example.test.TestCase 没有公共构造函数 TestCase(String name) 或 TestCase()

正如这条错误消息所说,您必须提供一个无参数构造函数或一个采用String arg 的构造函数。例如,您可以执行以下操作:

public TestCase() {
    super(MenuPage.class);
}

【讨论】:

    猜你喜欢
    • 2021-08-08
    • 2012-01-04
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 2015-04-25
    相关资源
    最近更新 更多