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?

1 Answer 1

7

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.