Spring boot WireMock未通过Spring引导集成测试拦截http请求
我使用Wiremock进行了Sprint引导集成测试,但出于某种原因,Wiremock没有提供存根响应,http请求将发送到实际的外部api。我错过什么了吗?我可以从日志中看到Wiremock服务器正在端口8888上启动Spring boot WireMock未通过Spring引导集成测试拦截http请求,spring-boot,integration-testing,wiremock,Spring Boot,Integration Testing,Wiremock,我使用Wiremock进行了Sprint引导集成测试,但出于某种原因,Wiremock没有提供存根响应,http请求将发送到实际的外部api。我错过什么了吗?我可以从日志中看到Wiremock服务器正在端口8888上启动 <dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock-jre8-standalone</
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<version>2.27.0</version>
<scope>test</scope>
</dependency>
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GatewayApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RegTypeIntegratedTest {
@LocalServerPort
private int port;
TestRestTemplate restTemplate = new TestRestTemplate();
HttpHeaders headers = new HttpHeaders();
ObjectMapper mapper = new ObjectMapper();
@Rule
public WireMockRule wireMockRule = new WireMockRule(options().port(8888));
@Test
public void testRegType()
throws JSONException, JsonParseException, JsonMappingException, FileNotFoundException, IOException {
wireMockRule.stubFor(post(urlPathMatching("{path}/.*/")).willReturn(
aResponse().withHeader("Content-Type", "application/json").withBody(new String(Files.readAllBytes(
Paths.get("path/regtypeResponse_stub.json"))))));
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
ResponseEntity<String> response = restTemplate.exchange(
createURLWithPort("/{service-url-path}y/regTypes?regtype=I"), HttpMethod.GET, entity,
String.class);
String expected = new String(Files
.readAllBytes(Paths.get("path/regtypeResponse_expected.json")));
JSONAssert.assertEquals(expected, response.getBody(), true);
}
private String createURLWithPort(String uri) {
return "http://localhost:" + port + uri;
}
}
com.github.tomakehurst
wiremock-jre8-standalone
2.27.0
测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes=GatewayApplication.class,webEnvironment=SpringBootTest.webEnvironment.RANDOM\u端口)
公共类RegTypeIntegratedTest{
@本地服务器端口
专用int端口;
TestRestTemplate restTemplate=新的TestRestTemplate();
HttpHeaders=新的HttpHeaders();
ObjectMapper mapper=新的ObjectMapper();
@统治
public WireMockRule WireMockRule=新的WireMockRule(options().port(8888));
@试验
公共void testRegType()
抛出JSONException、JsonParseException、JsonMappingException、FileNotFoundException、IOException{
stubFor(post(urlpathmatting(“{path}/*/”)).willReturn(
aResponse().withHeader(“内容类型”,“应用程序/json”).withBody(新字符串(Files.readAllBytes(
get(“path/regtypeResponse_stub.json”;);
HttpEntity=新的HttpEntity(空,标题);
ResponseEntity response=restemplate.exchange(
createURLWithPort(“/{service url path}y/regTypes?regtype=I”),HttpMethod.GET,entity,
字符串(类);
预期字符串=新字符串(文件)
.readAllBytes(path.get(“path/regtypeResponse_expected.json”);
assertEquals(应为response.getBody(),true);
}
私有字符串createURLWithPort(字符串uri){
返回“http://localhost:“+端口+uri;
}
}
我认为您混淆了SpringBootTest中运行的两个不同服务器的信息。一方面,您告诉您的单元测试让您的实际Spring Boot应用程序在随机web端口上运行测试,以模拟对您的webapp的实际调用。spring应用程序获得的这个端口分配给您,您正在拉入端口
变量
同时,您正在端口8888
上设置Wiremock,您也可以在日志中观察到该端口,如前所述
在您的测试中,您现在通过return”调用启动的spring boot应用程序的真实端口进行测试http://localhost:“+端口…
在调用RestTemplate实例时引用
我认为当你真的想调用你的running spring应用程序时,以及当你使用wiremock调用你的外部端点模拟时,你需要将两者分开。我认为你混淆了SpringBootTest中运行的两个不同服务器的信息。一方面,您告诉您的单元测试让您的实际Spring Boot应用程序在随机web端口上运行测试,以模拟对您的webapp的实际调用。spring应用程序获得的这个端口分配给您,您正在拉入
端口
变量
同时,您正在端口8888
上设置Wiremock,您也可以在日志中观察到该端口,如前所述
在您的测试中,您现在通过return”调用启动的spring boot应用程序的真实端口进行测试http://localhost:“+端口…
在调用RestTemplate实例时引用
我认为,当您真的想调用正在运行的spring应用程序时,以及当您使用wiremock调用外部端点模拟时,您需要将两者分开。您为wiremock服务器设置了
POST
方法,但随后在TestRestTemplate
客户端中调用了GET
方法
您也没有扩展路径变量
{service url path}
您为WireMock服务器插入了POST
方法,但随后在TestRestTemplate
客户机中调用了GET
方法
您也没有扩展path变量
{service url path}
我认为这是使用HTTP方法时出现一些疏忽的情况,但现在我和cobz一样困惑。你能解释一下为什么需要WireMock和Spring引导服务器吗?我原以为在使用HTTP方法时会出现一些疏忽,但现在我和cobz一样困惑。您能解释一下为什么需要WireMock和Spring引导服务器吗?我在这里测试的API是GET,但是从测试中的API调用的后端API(stubbed)是POST。我在这里测试的API是GET,但是后端API(stubbed)从测试中的API调用是POST。实际上,我在这里缺少基本概念,我没有配置后端API以转到Wiremock实例,因此它将转到real/live API。我的印象是,Wiremock在JUnit内部运行时会进行拦截。我将端点配置为Wiremock端点,所有的好东西我实际上都缺少基本概念,这里我没有配置后端api以转到Wiremock实例,因此它将转到real/live api。我的印象是,Wiremock在JUnit内部运行时会进行拦截。我将端点配置为Wiremock端点,一切正常