Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssertionFailedError: <class> has no public constructor

Tags:


I am working with Android Studio and I need to add a unit tests to my project.
I read various tutorials, but nothing hepled me.
My problem is:
TestXMLParser.java:

public class TestXMLParser extends ActivityInstrumentationTestCase2<HomePageActivity> {  public TestXMLParser(Class<HomePageActivity> activityClass) {     super(activityClass); }  @Override public void setUp() throws Exception {     super.setUp();      //Controller.init((Activity)getContext()); }  @Override public void tearDown() throws Exception {     super.tearDown(); }  public void testTrue() throws Exception {     assertTrue(true); } ... } 

When I run it, I see this message:

junit.framework.AssertionFailedError: Class cz.cvut.kosapp.jUnitTests.TestXMLParser has no public constructor TestCase(String name) or TestCase() at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661) 

I really do not know why. Other jUnit tests works well, for example when I use:

public class TestXMLParser extends AndroidTestCase { ... 

in header, this works and tests are running correctly.
But I need use the Context (as a Activity) to run other code (in Controller class).

Do you have any idea how fix it?
Thank you for your comments.

like image 756
dusanjencik Avatar asked Jul 11 '13 17:07

dusanjencik


Video Answer


1 Answers

You need to add either a default constructor or a constructor which takes a String as a parameter. Adding the following default constructor with a call to the base class constructor should work:

public TestXMLParser() {     super(HomePageActivity.class); } 
like image 93
p e p Avatar answered Oct 18 '22 17:10

p e p