0

I a quite new to Junit. I have a question on how Java runs Junit classes. I have a code like this

public class TestJunit1 {

        String message = "Srinivas";
        MessageUtil messageutil = new MessageUtil(message);

        @Test
        public void testPrintMessage() {
            System.out.println("Inside testPrintMessage");
            assertEquals(message, messageutil.printMessage());
        }

}

public class TestJUnit5 extends TestCase {
    protected double fValue1;
    protected double fValue2;

    @Before
    public void setUp() throws Exception {
        fValue1 = 2.0;
        fValue2 = 3.0;
    }

    @Test
    public void testAdding() {
        System.out.println("No of test cases =" + this.countTestCases());

        String name = this.getName();
        System.out.println("Test Case name is: "+name);

        this.setName("methodNewAdd");
        String newName = this.getName();
        System.out.println("New name of the test case is:"+newName);
        System.out.println(this.getClass());
    }

    @After
    public void tearDown() throws Exception {
    }

public class TestSuiteDemo {


    public static void main(String[] args) {

        TestSuite ts = new TestSuite(TestJunit1.class, TestJunit2.class, TestJUnit5.class);
        TestResult tr = new TestResult();

        ts.run(tr);
        System.out.println("Number of test cases is:"+tr.runCount());


    }

}

when I run TestSuiteDemo as Java application in eclipse, it produces output from println statements from TestJUnit5 and not from TestJunit1. Could somebody please explain why this happens?

Regards Srinivas

2 Answers2

1

Because TestJUnit5 extends a TestCase (not TestJunit1) so JUnit sees the test case and runs it.

From the junit.framework.TestSuite code, any class that inherits Test class or extends TestCase can get registered with TestSuite constructor.

private void addTestsFromTestCase(final Class<?> theClass) {
        fName = theClass.getName();
        try {
            getTestConstructor(theClass); // Avoid generating multiple error messages
        } catch (NoSuchMethodException e) {
            addTest(warning("Class " + theClass.getName() + " has no public constructor TestCase(String name) or TestCase()"));
            return;
        }

        if (!Modifier.isPublic(theClass.getModifiers())) {
            addTest(warning("Class " + theClass.getName() + " is not public"));
            return;
        }

        Class<?> superClass = theClass;
        List<String> names = new ArrayList<String>();
        while (Test.class.isAssignableFrom(superClass)) {
            for (Method each : MethodSorter.getDeclaredMethods(superClass)) {
                addTestMethod(each, names, theClass);
            }
            superClass = superClass.getSuperclass();
        }
        if (fTests.size() == 0) {
            addTest(warning("No tests found in " + theClass.getName()));
        }
    }

To get TestJunit1 to work, you will have to create an annotative Suite.

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
   TestJunit1.class,
   TestJunit2.class,
   TestJunit5.class
})

public class JunitTestSuite {  
}

And to run it locally:

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class JunitTestSuiteRunner {

    public static void main(String[] args) {

        Result result = JUnitCore.runClasses(JunitTestSuite.class);
        for (Failure fail : result.getFailures()) {
            System.out.println(fail.toString());
        }
        if (result.wasSuccessful()) {
            System.out.println("All tests finished successfully...");
        }
    }
}

I hope this helps.

Source reference.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
0

amoq reference to the stackoverflow post JUnit confusion: use 'extends TestCase' or '@Test'? is accurate. There are two main way for junit test to recognize and run your rest Junit version 3 , extend test case . Junit version 4 annotate with @Test. Since your test 1 has neither it is not run at all. Confirm the version of junit your eclipse is using, you could use the annotation version @Test which u might find more pleasant

Community
  • 1
  • 1