本文主要是介绍gradle学习笔记:gradle契约测试的执行命令,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
环境
gradle项目
测试框架:mockito
前言
个人笔记,非教程
配置生成测试代码的任务
// 用于生成 Mock 测试,即测试生成的 Mock 是否符合契约
task generateMockTests << {def contractBasePath = projectDir.getAbsolutePath() + '/src/test/java/com/xingren/pms/api/contract/AbstractContractTest.java'def contractTestDir = projectDir.getAbsolutePath() + '/gensrc/test/java/com/xingren/pms/api/contract/'def mockTestDir = projectDir.getAbsolutePath() + '/gensrc/test/java/com/xingren/pms/api/mock/'new File(mockTestDir).mkdirs()new File(contractTestDir).mkdirs()// 拷贝 TestBase 文件并设置为 MockTest 类别def updatedBaseContent = new File(contractBasePath).getText('UTF-8').replaceAll('package com.xingren.pms.api.contract', 'package com.xingren.pms.api.mock').replaceAll('class AbstractContractTest', 'class AbstractMockTest').replaceAll('ContractTest.class', 'com.xingren.pms.api.category.MockTest.class')def newBaseFile = new File(mockTestDir + 'AbstractMockTest.java')newBaseFile.write(updatedBaseContent, 'UTF-8')// 拷贝 ContractTest 文件并修改为 Mock 测试new File(contractTestDir).eachFileRecurse { file ->if (file.name.endsWith('Test.java') && !file.name.endsWith('MockTest.java')) {def newFile = new File(mockTestDir + file.name.replace('Test.java', 'MockTest.java'))def updatedContent = file.getText('UTF-8').replaceAll('package com.xingren.pms.api.contract', 'package com.xingren.pms.api.mock').replaceAll('io.restassured.module.mockmvc.RestAssuredMockMvc\\.\\*', 'io.restassured.RestAssured.*').replaceAll('module.mockmvc.specification.MockMvcRequestSpecification', 'specification.RequestSpecification').replaceAll('MockMvcRequestSpecification', 'RequestSpecification').replaceAll('Test extends AbstractContractTest', 'MockTest extends AbstractMockTest')newFile.write(updatedContent, 'UTF-8')}}
}generateMockTests.dependsOn generateContractTests
compileTestJava.dependsOn generateMockTests
生成测试代码命令
./gradlew :admin:generateContractTests -x vueBuild
-x 跳过某项任务
vueBuild是项目里配置的前端路径,如下:
task vueBuild(type: NpmTask) {workingDir = file("$projectDir/frontend")args = ['run', 'build']
}
执行契约测试
./gradlew :admin:test -x vueBuild
单独启动后端命令
在设置如下配置后:
bootRun {addResources = truedef env = 'env'systemProperty env, System.getProperty(env, 'DEV')
}
执行:
./gradlew clean :admin:bootRun -x vueBuild -x test -x check
这篇关于gradle学习笔记:gradle契约测试的执行命令的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!