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:
(3) Append the following code to your pom.xml:
<profiles> <profile> <id>SlowTests</id> <properties> <testcase.groups>com.sap.SlowTests</testcase.groups> </properties> </profile> <profile> <id>FastTests</id> <properties> <testcase.groups>com.sap.FastTests</testcase.groups> </properties> </profile> </profiles> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.13</version> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit47</artifactId> <version>2.13</version> </dependency> </dependencies> <configuration> <groups>${testcase.groups}</groups> </configuration> </plugin> </plugins> </build>
(4) In my project, by default all 7 test methods will be executed during Maven build:
Suppose you only want to execute unit test belonging to category “SlowTests”, use the following command line:
Since now I only marked one method with annotation SlowTests, only one test method is executed:
If you would like to execute all unit tests EXCEPT @SlowTests, simply add another profile in pom.xml:
<profile> <id>NonSlowTests</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <excludedGroups>com.sap.SlowTests</excludedGroups> </configuration> </plugin> </plugins> </build> </profile>
Before test, in order to prove that Slow method is NOT really executed, I add a system.out.println in each method:
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.
本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
ABAP模拟Java Spring依赖注入(Dependency injection)的一个尝试
Recently I will deliver a session regarding dependency inversion principle to my team. As Java Spring is already widely used in all other Java development teams in my site, some ABAPers are not well aware of its idea and implementation under the hood. In order for ABAPers to easily understand the mechanism of Java Spring dependency inversion, I wrote a prototype in ABAP after going through related Java source code of Spring. Before I start, I perform the search in SCN. There are already several ...
- 下一篇
使用Java JUnit框架里的@SuiteClasses注解管理测试用例
Suppose I have four test cases in my project, the total methods to be tested: 7 Based on the blog Run only given sets of your unit test via @Category, it is possible to organize test methods within THE SAME CLASS to different categories via @Category, that is, the granularity to control which test methods should be executed is method level. There is another annotation @SuiteClasses which can allows us to categorize test classes into different test suites, and once we specify a given test suite t...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- SpringBoot2整合Redis,开启缓存,提高访问速度
- CentOS7,8上快速安装Gitea,搭建Git服务器
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS7设置SWAP分区,小内存服务器的救世主