java 039 s rule_使用Java JUnit框架里的@Rule注解的用法举例

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.

466aff28c401b625bd3c756f96658920.png

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.

d214043bf73ddb656b6390819e39b7c1.png

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:

7ee36b5ba2da26197756f2770817bcf5.png

With the help of @Rule, we can achieve the same as @Test(expected=).

ca336f56b551788a50fb1c02a6ed7d8e.png

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.

fe318855c739e38f4b05c29ccdde3d8b.png

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()

{

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:

11a4ba92f15859824dbe9d43ec70bd2a.png

0a677d81e5bd7c547a5931b341698924.png

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

61fb9822a20bf853fa7bc619689492cf.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值