一、spock-groovy单元测试的五种情况
/**
- 单元测试
- given: mock单测中指定mock数据,模拟入参
- when: 触发行为,比如调用指定方法或函数
- then: 做出断言表达式
- expect: 期望的行为,when-then的精简版
- @since 2022-07-13
*/
@CodeBootTest
class Test extends Specification {
@Autowired
private TestService testService
// 对于Impl私有的方法,无法通过Service调用的,需要单独new一个,并设置其属性
def testServiceImpl = new TestServiceImpl();
def lovAdapter = Mock(LovAdapter)
// 运行前的启动方法
void setup() {
testServiceImpl.lovAdapter = lovAdapter
}
// 情况一:expect
def "test-1"(){
given:
PageRequest pageRequest = new PageRequest(0, 10)
QueryDto queryDto = new QueryDto()
queryDto.setTenantId(0L)
expect:
testService.selectList(pageRequest, queryDto).getContent().size() == 3
}
// 情况二:when-then
def "test-2"(){
given:
when:
Header result = testService.detail(33L, 0L)
then:
result.recNo == "20220707100001"
result.recHeaderId == 33L
}
// 情况三:thrown(Exception)
def "test-3"(){
given:
List<Header> list = new ArrayList<Header>();
Header vo = new Header();
vo.setRecHeaderId(33L)
vo.setRecNo("20220707100001")
vo.setRecStatus("2")
list.add(vo)
when:
testService.checkSuccess(list)
then:
thrown(IllegalArgumentException)
}
// 情况四:noExceptionThrown,mock模拟入参
def "test-4" () {
given:
Header vo = new Header();
vo.setTenantId(0L)
vo.setRecHeaderId(33L)
vo.setRecNo("20220707100001")
vo.setRecStatus("2")
vo.setIsIncludeTax("Y")
def list = Arrays.asList(vo)
//构造值集查询出参
lovAdapter.queryLovValue("YES_NO", vo.tenantId) >> Arrays.asList(new LovValueDTO(value: "Y", meaning: "是"))
when:
testServiceImpl.processData(list, vo.tenantId, null, null)
then:
noExceptionThrown()
}
// 情况五:expect,and-with验证结果
def "test-5" () {
given:
expect:
Header result = testService.detail(33L, 0L)
// 对于数据转换无返回值的,可以比较数据处理前后的值
and: "验证结果是否正确"
with(result) {
(result.recNo == "20220707100001")
(result.recHeaderId == 33L)
}
}
// 情况六:where 表格方式验证用户信息的合法性
def "test-6"() {
expect:
Math.max(a,b) == result
// 这样一个单元测试会验证两组数据
// 第一组 a = 1 , b = 2, result = 2
// 第二组 a = 3 , b = 0, result = 3
where:
a |b |result
1 |2 |2
3 |0 |3
}
}
二、配置文件及SQL注意点
1.application-test.yml在mybatisplus下面加上
liquibase:
change-log: classpath:db/changelog/db.mysql-master.xml
2.db.mysql-master.xml里加上对应单元测试脚本路径
3.mapper文件注意点:要用单引号,不能用双引号
4.测试脚本注意点
创建库(CREATE SCHEMA IF NOT EXISTS TEST;)
若需要设置库(SET SCHEMA TEST;)
建表语句(库名.表名,普通索引删除,COLLATE utf8_unicode_ci 以及CHARACTER SET utf8mb4 COLLATE utf8mb4_bin 这种删除,表编码设置ENGINE=InnoDB DEFAULT CHARSET=utf8 ;)
5.单元测试启动就是整个类启动,不支持单个方法启动
三、spock单元测试jar包
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.199</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-to-slf4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.spockframework</groupId> <artifactId>spock-core</artifactId> <scope>test</scope></dependency> <dependency> <groupId>org.spockframework</groupId> <artifactId>spock-spring</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>3.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> <scope>test</scope> </dependency>版权归原作者 linsa_pursuer 所有, 如有侵权,请联系我们删除。