Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "has no public TestCase(String name) or TestCase() in Junit test

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)

This is my code:

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

like image 402
gamo Avatar asked May 27 '14 04:05

gamo


2 Answers

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

by the following:

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

This constructor has no arguments as needed by the framework and showing in the code listing of your tutorial:

like image 157
Simulant Avatar answered Oct 21 '22 18:10

Simulant


If your problem is still unresolved: You can try this annotation by adding

 @RunWith (JUnit4.class)
like image 1
hpolat Avatar answered Oct 21 '22 18:10

hpolat