Suppose you need to repeatedly execute some test method in your unit test case, for example, you would like to test getPrice based on the first set of test data 5 times in test method test1() while for the second set of test data, only one time should be executed.
The below class RepeatDemoOne is a bad example, where this special LOOP operation is mixed with test method implementation.
Ideally the test method should only contain the pure logic to operate on the method being tested. So we have a better solution RepeatDemoTwo:
It could easily be observed that now the test method test1 and test2 are rather clean: no more for LOOP and System.out.println exist any more.
Instead, I put the LOOP logic and print out operation into class RepeatableRule which implements interface MethodRule. The concrete rule implementation is done by overriding method apply as below:
class RepeatableRule implements MethodRule{
int times = 1;
String[] testMethods = null;
RepeatableRule(int times, String[] testMethods){
this.times = times;
this.testMethods = testMethods;
}
@Override
public Statement apply(final Statement base, final FrameworkMethod method, Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
int loopTime = 1;
if(Arrays.asList(testMethods).contains(method.getName())) {
loopTime = times;
}
for(int i = 0; i < loopTime; i++ ) {
base.evaluate();
System.out.println(method.getName() + " executed.");
}
}
};
}
}
When I execute this test case, I can get exactly the same result as RepeatDemoOne:
With the help of @Rule, we can achieve the same as @Test(expected=).
For example, we can use an instance of class ExpectedException to manually declare within a test method itself that a test method expects a given type of exception class.
Besides exception, we can also manually specify a sub string which is expected to appear in an error message, and add our custom error message in Junit report if a test method fails. See following code for example:
public class RuleWithException {
@Rule
public ExpectedException exp = ExpectedException.none();
@Test
public void expectMessage()
{
exp.expectMessage("Hello World");
throw new RuntimeException("Hello World will throw exception.");
}
@Test
public void expectCourse()
{
exp.expectCause(new BaseMatcher<IllegalArgumentException>()
{
public boolean matches(Object item)
{
return item instanceof IllegalArgumentException;
}
@Override
public void describeTo(org.hamcrest.Description description) {
description.appendText("Expected exception with type IllegalArgumentException "
+ "raised in test method! ");
}
});
Throwable cause = new IllegalArgumentException("Cause Test.");
throw new RuntimeException(cause);
}
}
In this example, if we comment out line 46, the customed message defined in method describeTo will be printed out in JUnit console:
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
相关推荐
在JUnit中使用@Rule测试文件和目录Java开发Java经验技巧共3页.pdf.zip
@Rule 允许灵活添加或重新定义测试类中的每个测试方法的行为 @FixMethodOrder 指定测试方法的执行顺序 @ContextConfiguration 参数locations="classpath:spring-mybatis.xml" 指向src下的该文件 执行顺序: @...
Junit是java中测试的必备工具,Junit_test这个程序是更好的实现对Junit的了解。帮助大家学习
JUnit 类别 JUnit 4.9 引入了一个很好的注释来对您的测试进行分类。 而 TestNG 选择为使用字符串 JUnit 类别使用类。 这比字符串更灵活,这在使用多模块 maven 项目时可能会很痛苦(有关更多详细信息,请参阅 JUnit ...
介绍如何利用反射和注解去简单的模拟JUnit4单元测试的使用,之所以选择JUnit4是...需要注意的是这里并不是完全的模拟,只是简单实现了一下Runner类和JUnit注解相关的工作流程。所以本文的主要目是介绍反射和注解的使用
Junit java单元测试内部机制解析
junit-extension 背景 原生的Junit无法满足我们在自动化测试实践过程中的碰到一些需求,比如 ...以上三种注解和Junit build-in的注解@Test配合使用,示例如下 import org.junit.Test; import org.sdet.junit.extens
今天小编就为大家分享一篇关于Junit 5中@ParameterizedTest与@EnumSource结合使用,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
是实现同样的功能S2JUnit4与@rule产品。 操作环境 Java 6 JUnit 4.11 S2Tiger 2.4.47 如何使用 在测试类的公共字段中声明用@Rule 注释的S2TestRule。 // @RunWith(Seasar2.class) は不要 public class FooTest {...
NULL 博文链接:https://bijian1013.iteye.com/blog/2119792
Junit中的基本注解(教学视频) Junit中的基本注解,是必须掌握的。 @BeforeClass – 表示在类中的任意public static void方法执行之前执行 @AfterClass – 表示在类中的任意public static void方法执行之后执行 ...
java自定义 注解 annotation、标签库tag、监听listener、junit简单测试代码
1 Junit是什么 JUnit 是一个 Java 编程语言的单元测试框架。JUnit 在测试驱动的开发方面有很重要的发展,是起源于 JUnit 的一个统称为 xUnit 的单元测试框架之一。 2 单元测试概念 ...必须使注解@Test修饰
@Rule你可以使用类似@Rule 。如何使用@RunWith将Seasar2.class指定的Seasar2.class为Seasar24.class 。 @RunWith ( Seasar24 . class)public class FooTest { // ...}未实现它基本上只是从 Seasar2 复制和粘贴,所以...
org.junit.Rule.class org.junit.Test.class org.junit.experimental.ParallelComputer.class org.junit.experimental.categories.Categories.class org.junit.experimental.categories.Category.class org.junit....
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage,包含依赖包:junit-jupiter-5.5.1.jar,junit-jupiter-engine-5.5.1.jar,junit-jupiter-params-5.5.1.jar,junit-platform-launcher-1.5.1.jar,junit-...
JUnit4通过注解的方式来识别测试方法。目前支持的主要注解有(下列注解标示了单元测试的不同运行阶段执行的代码): @BeforeClass 全局只会执行一次,而且是第一个运行 @Before 在测试方法运行之前运行(每个测试...
使用 tk-mybatis 的 demo 测试了 Spring 的事务 包含 @Transactional 注解所有的功能测试。
DaggerMock A JUnit rule to easily override Dagger 2 objects More info about testing using Dagger 2 and Mockito are available in this ... @Rule public DaggerMockRule rule = new Dagger