0

FristService在内部调用SecondServicethrough feign client,我正在尝试编写测试FirstService并希望模拟对第二个服务的调用。

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

java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: second-service
    at org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient.execute(LoadBalancerFeignClient.java:90)
    at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:119)

这是测试代码

@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;
}

这是我的假客户,我在测试模式下没有改变任何东西

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

0 回答 0