Spring 云合同消费者测试问题
Spring Cloud Contract Consumer Test Issue
我正在测试 spring 云合约的消费者端。
供应商在这里:https://github.com/pkid/spring-cloud-contract-with-surefire.
提供程序生成的存根 jar 在这里:https://github.com/pkid/spring-cloud-contract-with-surefire-consumer/blob/master/sample-repo-service-1.0.0-SNAPSHOT-stubs.jar
当我运行消费者测试时(来源在这里:https://github.com/pkid/spring-cloud-contract-with-surefire-consumer):
@Test
public void shouldGiveFreeSubscriptionForFriends() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/greeting")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string("{\"id\":1,\"content\":\"Hello, World!\"}"));
}
当我执行 "mvn test" 时,我可以看到存根 jar 已正确找到并解压。但是我得到了端点 2“/greeting”不存在的错误(404)。
你能帮帮我吗?谢谢!
您正在使用 mockMvc
连接到 WireMock 实例。那行不通的。将消费者端的 mockMvc
更改为 restTemplate
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
@AutoConfigureMockMvc
@AutoConfigureJsonTesters
@DirtiesContext
@AutoConfigureStubRunner(ids = {"com.sap.ngp.test:sample-repo-service:+:stubs:8080"}, workOffline = true)
public class ConsumerTest {
@Test
public void shouldGiveFreeSubscriptionForFriends() throws Exception {
ResponseEntity<String> result = new TestRestTemplate().exchange(RequestEntity
.get(URI.create("http://localhost:8080/greeting"))
.header("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.build(), String.class);
BDDAssertions.then(result.getStatusCode().value()).isEqualTo(200);
BDDAssertions.then(result.getBody()).isEqualTo("{\"content\":\"Hello, World!\"}");
}
}
请阅读文档中的 mock mvc 的用途 http://docs.spring.io/spring-security/site/docs/current/reference/html/test-mockmvc.html
我正在测试 spring 云合约的消费者端。
供应商在这里:https://github.com/pkid/spring-cloud-contract-with-surefire.
提供程序生成的存根 jar 在这里:https://github.com/pkid/spring-cloud-contract-with-surefire-consumer/blob/master/sample-repo-service-1.0.0-SNAPSHOT-stubs.jar
当我运行消费者测试时(来源在这里:https://github.com/pkid/spring-cloud-contract-with-surefire-consumer):
@Test
public void shouldGiveFreeSubscriptionForFriends() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/greeting")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string("{\"id\":1,\"content\":\"Hello, World!\"}"));
}
当我执行 "mvn test" 时,我可以看到存根 jar 已正确找到并解压。但是我得到了端点 2“/greeting”不存在的错误(404)。
你能帮帮我吗?谢谢!
您正在使用 mockMvc
连接到 WireMock 实例。那行不通的。将消费者端的 mockMvc
更改为 restTemplate
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
@AutoConfigureMockMvc
@AutoConfigureJsonTesters
@DirtiesContext
@AutoConfigureStubRunner(ids = {"com.sap.ngp.test:sample-repo-service:+:stubs:8080"}, workOffline = true)
public class ConsumerTest {
@Test
public void shouldGiveFreeSubscriptionForFriends() throws Exception {
ResponseEntity<String> result = new TestRestTemplate().exchange(RequestEntity
.get(URI.create("http://localhost:8080/greeting"))
.header("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.build(), String.class);
BDDAssertions.then(result.getStatusCode().value()).isEqualTo(200);
BDDAssertions.then(result.getBody()).isEqualTo("{\"content\":\"Hello, World!\"}");
}
}
请阅读文档中的 mock mvc 的用途 http://docs.spring.io/spring-security/site/docs/current/reference/html/test-mockmvc.html