1.添加分组接口
SlowTests.java
package com.feng.baseframework.common;
/**
* baseframework
* 2019/8/19 17:59
* 执行慢的单元测试
*
* @author lanhaifeng
* @since
**/
public interface SlowTests {
}
FastTests.java
package com.feng.baseframework.common;
/**
* baseframework
* 2019/8/19 18:00
* 执行快的单元测试
*
* @author lanhaifeng
* @since
**/
public interface FastTests {
}
2.使用标注分组
在类上标注
package com.feng.baseframework.common;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
/**
*
* @ProjectName: svc-search-biz
* @Description: 单元测试基础类,用于继承
* @Author: 兰海峰
* @CreateDate: 2018/4/28 17:20
* @UpdateUser:
* @UpdateDate: 2018/4/28 17:20
* @UpdateRemark:
*/
@RunWith(MockitoJUnitRunner.class)
@Category(FastTests.class)
public class MockitoBaseTest {
@Test
public void mockitoTest(){
}
}
在方法上标注
/**
* 计算机中存储的都是数的补码
* 原码:二进制定点表示法,最高位为符号位,0表示正,1表示负,其余位数表示数值大小
* 正数的原码、反码、补码都是相同的
* 反码:负数反码是除符号位外,其余原码依次取反
* 补码:负数的补码是反码的末位加1
*
* 强制将高精度类型转为低精度类型会丢失精度
*/
@Test
@Category(FastTests.class)
public void typeConversionLowerTest(){
logger.info("130二进制为:"+Integer.toBinaryString(130));//00000000 00000000 00000000 10000010
logger.info("-2二进制为:"+Integer.toBinaryString(-2));//11111111 11111111 11111111 11111110
/**
* 1. 130为int,int为4个字节32位,故二进制为00000000 00000000 00000000 10000010
* 2. 强制转为byte,byte为1个字节8位,得到补码10000010
* 3. 最高一位为1,表示这是一个负数的补码
* 4. 由补码减去1,得到反码为10000001,由反码除去符号位依次取反得到原码11111110
* 5. 故结果为-126
*/
logger.info("(byte)130 is "+(byte)130);//-126
}
3.pom配置执行分组
<!-- 默认执行FastTests分组 -->
<properties>
<testcase.groups>com.feng.baseframework.common.FastTests</testcase.groups>
</properties>
<profiles>
<profile>
<id>SlowTests</id>
<properties>
<testcase.groups>com.feng.baseframework.common.SlowTests</testcase.groups>
</properties>
</profile>
<profile>
<id>FastTests</id>
<properties>
<testcase.groups>com.feng.baseframework.common.FastTests</testcase.groups>
</properties>
</profile>
<profile>
<id>AllTests</id>
<properties>
<testcase.groups>com.feng.baseframework.common.SlowTests,com.feng.baseframework.common.FastTests</testcase.groups>
</properties>
</profile>
</profiles>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<groups>${testcase.groups}</groups>
</configuration>
</plugin>
</plugins>
运行时,不手动指定激活配置则默认执行FastTests分组
指定分组执行
test -P SlowTests
test -P FastTests
test -P AllTests
参考:
https://cloud.tencent.com/developer/ask/115187
https://www.jianshu.com/p/ef69b4805788