Java Spring boot WireMock junit5不模拟外部调用

Java Spring boot WireMock junit5不模拟外部调用,java,spring-boot,integration-testing,junit5,wiremock,Java,Spring Boot,Integration Testing,Junit5,Wiremock,我的FirstService正在通过外部客户端内部调用SecondService,我正在尝试为FirstService编写测试,并希望模拟对第二个服务的调用 似乎wiremock无法截获并响应模拟结果,但抛出以下异常(因为它不是模拟) 这是测试代码 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ContextConfiguration(initializers = {WireMockIni

我的
FirstService
正在通过
外部客户端内部调用
SecondService
,我正在尝试为
FirstService
编写测试,并希望模拟对第二个服务的调用

似乎
wiremock
无法截获并响应模拟结果,但抛出以下异常(因为它不是模拟)

这是测试代码

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = {WireMockInitializer.class})
@AutoConfigureMockMvc
public class TestFirstController {

    @Autowired
    private WireMockServer wireMockServer;

    @Inject
    public MockMvc mockMvc;

    @LocalServerPort
    private Integer port;

    @AfterEach
    public void afterEach() {
        this.wireMockServer.resetAll();
    }

    @Test
    public void testSomeMethod() throws Exception {
        this.wireMockServer.stubFor(
                WireMock.get(urlPathMatching("/second-controller/1"))
                        .willReturn(aResponse()
                                .withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
                                .withBody("[{\"id\":1,\"message\":\"Child title 1\"}]"))
        );

        mockMvc.perform(MockMvcRequestBuilders.request(HttpMethod.GET, "/first-controller/1"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(status().isOk());

    }
}
下面是我的业务方法,它首先从数据库中获取记录,然后调用第二个服务

@Override
public First getFirst(Integer id) {
    First first = map(respository.findById(id));
    
    //feign-client call
    List<Second> seconds = client.getSeconds(id);
    first.setChild(seconds);

    return first;
}

我通常会编写模拟实现的假客户端接口,并在测试中将其设置为@Primary,这对我来说非常方便。你能详细说明一下吗?你能展示你的假客户端的相关部分吗?在测试过程中,你在哪里覆盖URL以指向本地WireMock URL?@rieckpil这可能是我正在寻找的答案,我的理解是,当您使用
wire mock
时,您不必模拟任何其他内容,因此我没有在测试模式中更改任何针对外国客户机的内容。我还将添加外部客户端的代码。您必须在某个地方调整HTTP客户端的基本URL,使其不指向例如www.supercompany.com/api/products,但是
@Override
public First getFirst(Integer id) {
    First first = map(respository.findById(id));
    
    //feign-client call
    List<Second> seconds = client.getSeconds(id);
    first.setChild(seconds);

    return first;
}
@FeignClient("service-2")
public interface SecondApi {
    @GetMapping({"/second-controller/{id}"})
    SomeData getData(@PathVariable("id") Integer id);
}