在JUnit中使用TestSuite时,@ Rule注释不起作用 [英] @Rule annotation doesn't work while using TestSuite in JUnit
问题描述
我有3个测试类,如下所示:
i have 3 test classes as follows :
package com.junittest.testcases;
package com.junittest.testcases;
Test3类:
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类:
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类:
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));
}
}
还有一个TestSuite类:
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;
}
}
问题在于,当从Eclipse中从JUnit运行TestSuite1类时,它忽略了Test3类中的@Rule标记,因此输出与预期不符.为什么忽略@Rule标签.是否有执行这三种情况的替代方法?
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?
推荐答案
您正在混合JUnit 3(junit.framework.*
)和JUnit 4(org.junit.*
)代码.如果仅使用JUnit 4,该问题应该会消失.
You are mixing JUnit 3 (junit.framework.*
) and JUnit 4 (org.junit.*
) code. The problem should disappear if you are using JUnit 4 only.
- 删除
extends TestCase
(它们很简单,不需要,因为测试用@Test
注释. - 将
@Before
批注添加到setUp()
方法中. - 使用JUnit4套件: https://github.com/junit-team/junit /wiki/Aggregating-tests-insuites
- Remove
extends TestCase
(they are simple not needed, because your tests are annotated with@Test
. - Add the
@Before
annotation to yoursetUp()
methods. - Use JUnit4's Suite: https://github.com/junit-team/junit/wiki/Aggregating-tests-in-suites
这篇关于在JUnit中使用TestSuite时,@ Rule注释不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!