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
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:
If your problem is still unresolved: You can try this annotation by adding
@RunWith (JUnit4.class)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With