zoukankan      html  css  js  c++  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的原创文章,请关注公众号"汪子熙":

  • 相关阅读:
    Cf的一些总结
    Goodbye 2019
    牛客多校第8场 A题
    19牛客多校第二场 H题
    Hihocoder1673
    记一次根据图片原尺寸设置样式,并进行缩放和拖拽
    鱼骨时间轴案例(转自CSDN,原文链接附于文中)
    jQuery横向上下排列鱼骨图形式信息展示代码时光轴样式(转自CSDN,原文链接附于文中)
    mxGraph实现鱼骨图(因果图)(转自CSDN,链接附于文中)
    erlang win64位包下载链接
  • 原文地址:https://www.cnblogs.com/sap-jerry/p/13607043.html
Copyright © 2011-2022 走看看