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.
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2703836/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JUnit 註解@Category的工作原理Go
- JUnit 註解@RunWith的工作原理
- JUnit 註解@SuiteClasses的工作原理UI
- JUnit 註解@Rule的工作原理
- Spring框架裡註解@Autowired的工作原理Spring框架
- 使用Java JUnit框架裡的@Rule註解的用法舉例Java框架
- 使用Java JUnit框架裡的@SuiteClasses註解管理測試用例Java框架UI
- Angular @Injectable 註解的工作原理淺析Angular
- Java註解與原理分析Java
- JAVA 註解的基本原理Java
- Angular單元測試框架裡API toHaveBeenCalledTimes的工作原理Angular框架API
- 【Android】註解框架(二) 基礎知識(Java註解)& 執行時註解框架Android框架Java
- Java ”框架 = 註解 + 反射 + 設計模式“ 之 註解詳解Java框架反射設計模式
- Category的實現原理Go
- SAP Fiori程式設計模型規範裡註解 - @OData.publish工作原理解析程式設計模型
- 結合 category 工作原理分析 OC2.0 中的 runtimeGo
- JUnit5註解學習指引
- 三大框架的工作原理框架
- SAP Fiori @OData.publish 註解的工作原理解析
- SAP Fiori Elements 框架裡 Smart Table 控制元件的工作原理介紹框架控制元件
- 從原始碼解讀Category實現原理原始碼Go
- 暑期自學 Day 07 | Junit,反射,註解(一)反射
- 暑期自學 Day 08 | Junit,反射,註解(二)反射
- Java HashMap的工作原理JavaHashMap
- 從 java 註解分析 ButterKnife 工作流程Java
- SAP GUI裡Screen Painter的工作原理GUIAI
- 深入介紹 UI5 框架裡 Smart Field 控制元件的工作原理UI框架控制元件
- ObjC中Category的原理簡析OBJGo
- 如何在SAP Spartacus category 頁面裡拿到當前的category資訊Go
- Spring系列之新註解配置+Spring整合junit+註解注入Spring
- iOS底層原理-CategoryiOSGo
- 基於 java 註解的 csv 檔案讀寫框架Java框架
- Java學習之註解Annotation實現原理Java
- Transaction註解原理
- Angular @Inject 註解的實際應用例子和工作原理淺析Angular
- SAP Fiori 註解 @ObjectModel.readOnly工作原理解析Object
- Java中的註解-自定義註解Java
- item category 配置裡的溝溝坎坎Go