java category_Java JUnit框架里@Category注解的工作原理

Suppose you have a large number of unit test cases and you don’t want them to be executed all at the same time during Maven build. You can simply achieve it via annotation @Category.

(1) Create empty class FastTests and SlowTests. (2) In your test case class, categorize your test method using @Category annotation:

7e7dc8731da0ce49089200cabf74c850.png

(3) Append the following code to your pom.xml:

SlowTests

com.sap.SlowTests

FastTests

com.sap.FastTests

org.apache.maven.plugins

maven-compiler-plugin

3.1

1.8

1.8

org.apache.maven.plugins

maven-surefire-plugin

2.13

org.apache.maven.surefire

surefire-junit47

2.13

${testcase.groups}

(4) In my project, by default all 7 test methods will be executed during Maven build:

4ebacd824ec9556165a1b91a395c96ee.png

Suppose you only want to execute unit test belonging to category “SlowTests”, use the following command line:

080c1717e40b4b50983ba3092395edfa.png

Since now I only marked one method with annotation SlowTests, only one test method is executed:

f3ed025f210fcf1bfa51b5f83ed68cf3.png

If you would like to execute all unit tests EXCEPT @SlowTests, simply add another profile in pom.xml:

NonSlowTests

org.apache.maven.plugins

maven-surefire-plugin

com.sap.SlowTests

Before test, in order to prove that Slow method is NOT really executed, I add a system.out.println in each method:

481607b50f7aa5265ccb6f8843552149.png

Use command line: mvn test -P NonSlowTests From console output, I can ensure that the method with @Category(SlowTests.class) is NOT executed at all.

d67e68f9560fdaaae895b15a0ea34b94.png

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

6b82bd399e94519119a18c06e0b5a17a.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值