使用spring boot和mockmvc,我具有以下@beforeEach
方法的测试类:
@BeforeEach
void setUp(WebApplicationContext context,
RestDocumentationContextProvider restDocumentation) {
MockMvcRestDocumentationConfigurer configurer = documentationConfiguration(restDocumentation);
configurer.operationPreprocessors()
.withRequestDefaults(prettyPrint())
.withResponseDefaults(prettyPrint());
configurer.snippets()
.withDefaults(
httpRequest(),
httpResponse()
);
this.mockMvc = MockMvcBuilders.webAppContextSetup(context)
.apply(configurer)
.build();
和以下测试方法:
@Test
void createAdminHttpRequest() throws Exception {
var adminDTO = HandlerTestObjectGenerator.createFixedAdminDTO();
mockMvc.perform(
RestDocumentationRequestBuilders
.post("/api/admins")
.content(objectMapper.writeValueAsString(adminDTO))
.contentType(MediaType.APPLICATION_JSON_UTF8)
).andExpect(status().isCreated())
.andDo(document("create-admin",
preprocessRequest(),
preprocessResponse(),
requestFields(
usernameFieldDescriptor,
passwordFieldDescriptor,
rolesFieldDescriptor
),
responseFields(
admin_adminIdFieldDescriptor,
admin_usernameFieldDescriptor,
admin_rolesFieldDescriptor
),
SpringCloudContractRestDocs.dslContract(),
));
}
此测试效果很好,并且可以生成Spring Rest文档文档和常规合同。但是对于前端(反应)测试,我需要生成独立于框架的Pact.io合同。
问题因此,我的问题是,是否可以使用单个@Test
方法同时生成spring rest文档和pact.io pact?
我的研究到目前为止,我发现使用@Pact
注释的方法使用其自己的rest生成器可以生成协定。此外,我发现了此对话:https://gitter.im/spring-cloud/spring-cloud-contract/archives/2018/08/06,并且我正在尝试实现自己的maven插件以将groovy合同转换为pact,但是BodyConverter
类中似乎有错误,并且出现了以下异常:
java.lang.UnsupportedOperationException: use the array(String name) form
at au.com.dius.pact.consumer.dsl.PactDslJsonBody.array(PactDslJsonBody.java:673)
我的maven插件代码示例:初始化:
private PactContractConverter pactContractConverter = new PactContractConverter();
private ContractVerifierDslConverter contractDslConverter = new ContractVerifierDslConverter();
转换:
private void processFiles(List<File> contractFiles) throws Exception {
for(File file : contractFiles) {
logger.info("Processing " + file.getAbsolutePath());
Collection<Contract> contracts = contractDslConverter.convertFrom(file);
Collection<Pact> pacts = pactContractConverter.convertTo(contracts);
String jsonPacts = mapper.writeValueAsString(pactContractConverter.store(pacts));
File pactsFile = new File(outputDir, file.getName() + "_pact.json");
FileUtils.writeByteArrayToFile(pactsFile, jsonPacts.getBytes());
logger.info("Generated pact file: " + pactsFile.getAbsolutePath());
}
}
但是我得到了上面提到的例外。有直接调用该方法的方法,该方法抛出UnsupportedOperationException
。我找到了其他方法array(String name)
,但似乎未从转换器代码中调用。
让我们开始声明:
但是对于前端(反应)测试,我需要生成独立于框架的Pact.io合同。
您可以在多语言世界中使用Spring Cloud Contract。只需使用Docker(https://spring.io/blog/2018/02/13/spring-cloud-contract-in-a-polyglot-world)和https://cloud.spring.io/spring-cloud-static/spring-cloud-contract/2.2.0.RELEASE/reference/html/docker-project.html
回到您的问题
所以,我的问题是,是否可以使用单个@Test方法生成spring rest文档和pact.io pact?
让我们以不同的方式进行操作...由于您已经有了DSL,所以我想您也希望获得Pact文件。如果查看本节(https://cloud.spring.io/spring-cloud-static/spring-cloud-contract/2.2.0.RELEASE/reference/html/howto.html#how-to-generate-pact-from-scc)下的文档,您将确切看到问题的答案。添加一个插件就足够了,在测试生成后,DSL会将这些DSL转换为其他内容,例如契约文件。
使用Maven插件的示例
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>convert-dsl-to-pact</id>
<phase>process-test-classes</phase>
<configuration>
<classpathScope>test</classpathScope>
<mainClass>
org.springframework.cloud.contract.verifier.util.ToFileContractsTransformer
</mainClass>
<arguments>
<argument>
org.springframework.cloud.contract.verifier.spec.pact.PactContractConverter
</argument>
<argument>${project.basedir}/target/pacts</argument>
<argument>
${project.basedir}/src/test/resources/contracts
</argument>
</arguments>
</configuration>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
</plugin>
如果修改${project.basedir}/src/test/resources/contracts
以指向从REST Docs测试中转储DSL的位置,则将PACT文件转储到${project.basedir}/target/pacts
。在下面,您可以找到Gradle的类似示例
task convertContracts(type: JavaExec) {
main = "org.springframework.cloud.contract.verifier.util.ToFileContractsTransformer"
classpath = sourceSets.test.compileClasspath
args("org.springframework.cloud.contract.verifier.spec.pact.PactContractConverter",
"${project.rootDir}/build/pacts", "${project.rootDir}/src/test/resources/contracts")
}