Junit TestRule
来源:原创 发布时间:2015-04-13 归档:junit
环境 :
JDK 7
Maven 3
Junit 4.11
Eclipse Luna
TestRule
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public class LoggerRule implements TestRule {
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
String method = description.getMethodName();
Logger.debug("---> " + method + " run before");
base.evaluate();
Logger.debug("---> " + method + " run after");
}
};
}
}
@Rule
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
public class RuleUsageTest {
@Rule
public TestRule loggerRule = new LoggerRule();
@Test
public void testMethod() {
System.out.println("---> testMethod is running");
}
}
TestRule 类似于拦截器的作用, 它允许你在测试用例执行前后添加一些额外的操作。@Rule 要求必须为 public 修饰符修饰, 且不能是静态以及类型必须为 TestRule 类型。RuleUsageTest --> Run As JUnit Test 执行结果 :
---> testMethod run before
---> testMethod is running
---> testMethod run after
---> testMethod is running
---> testMethod run after
看起来用 @Before, @After, @BeforeClass, @AfterClass 也可以实现同样的效果。但如果测试有多处需要用到同样的逻辑, 使用 TestRule 可以避免重复编写同样的逻辑代码。
@ClassRule
public class ATest {
@Test
public void testAMethod() {
System.out.println("---> testAMethod is running");
}
}
public class BTest {
@Test
public void testBMethod() {
System.out.println("---> testBMethod is running");
}
}
@RunWith(Suite.class)
@SuiteClasses({ATest.class, BTest.class})
public class CTestSuite {
@ClassRule
public static TestRule rule = new TestRule() {
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Logger.debug("---> run junit test before");
base.evaluate();
Logger.debug("---> run junit test after");
}
};
}
};
}
@ClassRule 要求必须为 public static TestRule 修饰。CTestSuite --> Run As JUnit Test 执行结果 :
---> run junit test before
---> testAMethod is running
---> testBMethod is running
---> run junit test after
RuleChain
---> testAMethod is running
---> testBMethod is running
---> run junit test after
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public class RuleChainUsageTest {
@Rule
public TestRule chain = RuleChain.outerRule(new LoggerRule()).around(
new TestRule() {
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
System.out.println("------------------------------");
base.evaluate();
System.out.println("------------------------------");
}
};
}
}
);
@Test
public void testMethod() {
System.out.println("---> testMethod is running");
}
}
RuleChainUsageTest --> Run As JUnit Test 执行结果 :
---> testMethod run before
------------------------------
---> testMethod is running
------------------------------
---> testMethod run after
------------------------------
---> testMethod is running
------------------------------
---> testMethod run after
示例代码下载