Error testing JUnit service

the code:

public class Testprogdi extends ActivityInstrumentationTestCase2 { public Testprogdi(String pkg, Class activityClass) { super(pkg, activityClass); // TODO Auto-generated constructor stub } Context mContext; Registration reg = new Registration(); public void setUp(){ try { super.setUp(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } mContext = this.getInstrumentation().getContext(); } public void test(){ Assert.assertNotNull(reg.pass_url());} public void test1(){ Assert.assertTrue(reg.pass_url().startsWith("www"));} public void test2(){ Assert.assertTrue(reg.pass_url().startsWith("http")); } } 

An exception:

 junit.framework.AssertionFailedError: Class com.android.deviceintelligence.test.Testprogdi has no public constructor TestCase(String name) or TestCase() at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)` 

I get the same error for all my test classes.

+4
source share
2 answers

TestCase must have a public no-arg constructor or a constructor with a single String parameter.

You must remove your public Testprogdi constructor (String pkg, Class activityClass) and do any initialization in the setUp () method or add

 public Testprogdi() {} 

or

 public Testprogdi(String name) { // initialization } 

Btw, your test will be more maintainable if you make some other changes (not related to the first problem):

Give more meaningful names to your testing methods.

No need to catch (exception e) in setUp ().

I do not see how the tests test1 () and test2 () can pass.

+4
source

Eclipse provides you with constructor templates, for example:

 public Testprogdi(String pkg, Class<progdi> activityClass) { super(pkg, activityClass); // TODO Auto-generated constructor stub } 

or

 public Testprogdi(Class<progdi> activityClass) { super(activityClass); // TODO Auto-generated constructor stub } 

progdi is the name of your activity class. But as the textbook says, the constructor should be something like this:

 public Testprogdi() { super("com.progdi", progdi.class); // TODO Auto-generated constructor stub } 

and you forgot the parameter here:

 public class Testprogdi extends ActivityInstrumentationTestCase2<progdi> {} 
+2
source

Source: https://habr.com/ru/post/1400377/


All Articles