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.
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2722716/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Java JUnit框架裡@Category註解的工作原理Java框架Go
- JUnit 註解@RunWith的工作原理
- JUnit 註解@SuiteClasses的工作原理UI
- JUnit 註解@Rule的工作原理
- Spring框架裡註解@Autowired的工作原理Spring框架
- Angular @Injectable 註解的工作原理淺析Angular
- Category的實現原理Go
- JUnit5註解學習指引
- SAP Fiori @OData.publish 註解的工作原理解析
- Spring系列之新註解配置+Spring整合junit+註解注入Spring
- 從原始碼解讀Category實現原理原始碼Go
- SAP Fiori 註解 @ObjectModel.readOnly工作原理解析Object
- ObjC中Category的原理簡析OBJGo
- 使用Java JUnit框架裡的@Rule註解的用法舉例Java框架
- 暑期自學 Day 08 | Junit,反射,註解(二)反射
- 暑期自學 Day 07 | Junit,反射,註解(一)反射
- iOS底層原理-CategoryiOSGo
- iOS 開發:『Runtime』詳解(三)Category 底層原理iOSGo
- Angular @Inject 註解的實際應用例子和工作原理淺析Angular
- @LoadBalanced註解原理
- Transaction註解原理
- 使用Java JUnit框架裡的@SuiteClasses註解管理測試用例Java框架UI
- iOS底層原理總結 - Category的本質iOSGo
- JAVA 註解的基本原理Java
- SpringMVC工作原理詳解SpringMVC
- Java註解與原理分析Java
- JUnit5學習之五:標籤(Tag)和自定義註解
- 代理伺服器的工作原理詳解伺服器
- SAP Fiori程式設計模型規範裡註解 - @OData.publish工作原理解析程式設計模型
- Struts2工作原理(圖解)圖解
- SpringBoot原始碼解析-@ConditionalOnXXX註解原理Spring Boot原始碼
- Category:從底層原理研究到面試題分析Go面試題
- Mirror 的工作原理
- LiveData的工作原理LiveData
- OAuth的工作原理OAuth
- Feign的工作原理
- Spark的工作原理Spark
- 工作 6 年,@Transactional 註解用的一塌糊塗