Search code examples
junitjunit3

Junit3 suite.addTest(new mytest()) get 'null' error


I create a test suite and want to add some new tests, but suite.addTest() method seems doesn't work as I expected:

public static Test suite() {
    TestSuite suite = new TestSuite(AllTests.class.getName());
    //$JUnit-BEGIN$
    // This line works
    suite.addTestSuite(TestAdd.class);
    // This line will cause a 'null' failure
    suite.addTest(new TestAdd());
    //$JUnit-END$
    return suite;
}

Code of TestAdd:

import junit.framework.TestCase;
public class TestAdd extends TestCase { 
    public void test1()
    {
        assertEquals(1, 1);
    }
}

Did I missed something?


Solution

  • From the Javadocs:

    TestCase()

    No-arg constructor to enable serialization. This method is not intended to be used by mere mortals without calling setName().

    TestCase(java.lang.String name)

    Constructs a test case with the given name.

    It appears you must use the TestCase constructor passing the test a name string. Using the no-arg constructor is intended to enable serialization of the test cases only.

    Try:

    suite.addTest(new TestAdd("add"));