2

i have 3 test classes as follows :

package com.junittest.testcases;

Test3 class :

import junit.framework.TestCase;

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import annotations.DataSetRule;
import annotations.DataSets;
import com.junittest.test.MyClass;

public class Test3 extends TestCase{
    MyClass m = new MyClass();
    String group = "user";

    @Rule
    public TestRule dataSetRule = new DataSetRule();

    @Test
    public void test5() {
        System.out.println("In Test 5");
        //assertEquals(5, m.add(2,3));
    }

    @Test
    public void test6() throws Exception {
        org.junit.Assume.assumeTrue(group.equals("user"));
        System.out.println("In Test 6");
        //assertEquals(-2, m.sub(2,3));
    }

    @DataSets({"dataset","dataset1"})
    @Test
    public void test7() {
        System.out.println("In Test 7");
    }

    @Ignore
    @Test
    public void test8() {
        System.out.println("in test8");
    }
}

Test2 Class :

package com.junittest.testcases;

import org.junit.Test;

import singletons.SingletonClass;

import com.junittest.test.MyClass;

import junit.framework.TestCase;

public class Test2 extends TestCase{
MyClass m = new MyClass();
    public void setUp() {
        SingletonClass sc = SingletonClass.getInstance();
        System.out.println(SingletonClass.callCount);
    }

    @Test
    public void test3() {
        assertEquals(5, m.add(2,3));
    }

    @Test
    public void test4() {
        assertEquals(-2, m.sub(2,3));
    }
}

Test1 class :

package com.junittest.testcases;

import org.junit.Test;

import singletons.SingletonClass;

import com.junittest.test.MyClass;

import junit.framework.TestCase;

public class Test1 extends TestCase{
    MyClass m = new MyClass();

    public void setUp() {
        SingletonClass sc = SingletonClass.getInstance();
        System.out.println(SingletonClass.callCount);
    }

    @Test
    public void test1() {
        assertEquals(4, m.add(2,3));
    }

    @Test
    public void test2() {
        assertEquals(-1, m.sub(2,3));
    }


}

And a TestSuite class :

package com.junittest.testcases;
import rules.SomeTest;
import singletons.SingletonClass;
import junit.framework.Test;
import junit.framework.TestSuite;

public class TestSuite1 {
    public static Test suite() {
        SingletonClass sc = SingletonClass.getInstance();
        System.out.println(SingletonClass.callCount);

        TestSuite suite = new TestSuite(TestSuite.class.getName());
        suite.addTestSuite(Test1.class);
        suite.addTestSuite(Test2.class);
        suite.addTestSuite(Test3.class);
        return suite;
    }
}

The problem is that it is ignoring @Rule tag in Test3 class while running TestSuite1 class from JUnit from eclipse and thus the output is not as expected. Why is it ignoring the @Rule tag. Is there any alternative to execute all three cases?

Nishant Lakhara
  • 2,295
  • 4
  • 23
  • 46

1 Answers1

6

You are mixing JUnit 3 (junit.framework.*) and JUnit 4 (org.junit.*) code. The problem should disappear if you are using JUnit 4 only.

  1. Remove extends TestCase (they are simple not needed, because your tests are annotated with @Test.
  2. Add the @Before annotation to your setUp() methods.
  3. Use JUnit4's Suite: https://github.com/junit-team/junit/wiki/Aggregating-tests-in-suites
Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72