thej
thej

Reputation: 648

Error in JUnit testing of service

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

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 am getting the same error for all my test classes.

Upvotes: 4

Views: 4794

Answers (2)

laplasz
laplasz

Reputation: 3497

Eclipse gives you constructor templates like:

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 tutorial says the contsructor should be something like this:

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

and you forget the parameter here:

public class Testprogdi extends ActivityInstrumentationTestCase2<progdi> {}

Upvotes: 2

Fabricio Lemos
Fabricio Lemos

Reputation: 2955

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

You should delete your public Testprogdi(String pkg, Class activityClass) constructor and do any kind of initialization in the setUp() method or add

public Testprogdi() {}

or

public Testprogdi(String name) {
    // initialization 
}

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

Give more meaningful names to your test methods.

There is no need to catch (Exception e) in the setUp().

I don´t see how test1() and test2() can both pass.

Upvotes: 4

Related Questions