Kre*_*ase 26 java junit unit-testing junit4
目前,我的所有JUnit测试都是从提供标记有注释@BeforeClass
和@AfterClass
注释的方法的公共基类扩展而来的 - 所有这些都是为了要使用的测试设置一堆静态资源/服务.
由于以下几个原因,这对我来说似乎很尴尬:
@BeforeClass
和@AfterClass
调用,减慢测试速度 - 我们真的应该只调用一次我想要做的是以某种方式将当前的BeforeClass/AfterClass逻辑移出继承链,并将其转换为可以由单个测试和整个套件共享的内容.
可以这样做吗?如果是这样,怎么样? (如果重要的话,我正在使用JUnit 4.7,更新到不同版本可能很难卖)
Kre*_*ase 30
第一个问题的解决方案是将逻辑移动到org.junit.rules.ExternalResource
通过@ClassRule
JUnit 4.9中引入的连接到测试的扩展:
public class MyTest {
@ClassRule
public static final TestResources res = new TestResources();
@Test
public void testFoo() {
// test logic here
}
}
public class TestResources extends ExternalResource {
protected void before() {
// Setup logic that used to be in @BeforeClass
}
protected void after() {
// Setup logic that used to be in @AfterClass
}
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式,先前由基类管理的资源被移出测试类层次结构,并移动到更多模块化/可消耗的"资源"中,这些资源可以在类运行之前创建并在类运行之后销毁.
至于同时解决这两个问题 - 即:具有与单个测试的一部分相同的高级设置/拆卸运行以及作为套件的一部分 - 似乎没有任何特定的内置支持. 但是......,你可以自己实现它:
只需将@ClassRule
资源创建更改为工厂模式,该模式在内部引用计数以确定是否创建/销毁资源.
例如(请注意这是粗略的,可能需要一些调整/错误处理的稳健性):
public class TestResources extends ExternalResource {
private static int refCount = 0;
private static TestResources currentInstance;
public static TestResources getTestResources () {
if (refCount == 0) {
// currentInstance either hasn't been created yet, or after was called on it - create a new one
currentInstance = new TestResources();
}
return currentInstance;
}
private TestResources() {
System.out.println("TestResources construction");
// setup any instance vars
}
protected void before() {
System.out.println("TestResources before");
try {
if (refCount == 0) {
System.out.println("Do actual TestResources init");
}
}
finally {
refCount++;
}
}
protected void after() {
System.out.println("TestResources after");
refCount--;
if (refCount == 0) {
System.out.println("Do actual TestResources destroy");
}
}
}
Run Code Online (Sandbox Code Playgroud)
您的套件/测试类都只是@ClassResource
通过工厂方法使用该资源:
@RunWith(Suite.class)
@SuiteClasses({FooTest.class, BarTest.class})
public class MySuite {
@ClassRule
public static TestResources res = TestResources.getTestResources();
@BeforeClass
public static void suiteSetup() {
System.out.println("Suite setup");
}
@AfterClass
public static void suiteTeardown() {
System.out.println("Suite teardown");
}
}
public class FooTest {
@ClassRule
public static TestResources res = TestResources.getTestResources();
@Test
public void testFoo() {
System.out.println("testFoo");
}
}
public class BarTest {
@ClassRule
public static TestResources res = TestResources.getTestResources();
@Test
public void testBar() {
System.out.println("testBar");
}
}
Run Code Online (Sandbox Code Playgroud)
运行单个测试时,引用计数不会产生任何影响 - "实际初始化"和"实际拆解"只会发生一次.当通过套件运行时,套件将创建TestResource,并且各个测试将仅重用已经过时的测试(refcounting使其不会被实际销毁并在套件中的测试之间重新创建).