如何在Spring Boot应用程序中的Feign客户端上使用WireMock?

L42*_*L42 7 java spring spring-boot wiremock feign

我有一个使用Feign客户端的课程.之前我使用过Mockito并为Feign客户端中的每个方法调用提供了存储响应.现在我想使用WireMock,以便我可以看到我的代码正确处理不同类型的响应代码.我该怎么做呢?我无法弄清楚如何在测试中连接我的Feign客户端,并将其连接起来,以便它使用Wiremock而不是我在我的application.yml文件中设置的URL .任何指针都将非常感激.

Mat*_*nkt 5

Maybe you want to look at this project https://github.com/ePages-de/restdocs-wiremock

This helps you generate and publish wiremock snippets in your spring mvc tests (using spring-rest-docs).

Finally you can use these snippets to start a wiremock server to serve these recorded requests in your test.

If you shy away from this integrated solution you could just use the wiremock JUnit rule to fire up your wiremock server during your test. http://wiremock.org/docs/junit-rule/

Here is a sample test that uses a dynamic wiremock port and configures ribbon to use this port: (are you using feign and ribbon?)

    @WebAppConfiguration
    @RunWith(SpringRunner.class)
    @SpringBootTest()
    @ActiveProfiles({"test","wiremock"})
    public class ServiceClientIntegrationTest {

        @Autowired //this is the FeignClient service interface
        public ServiceClient serviceClient;

        @ClassRule
        public static WireMockRule WIREMOCK = new WireMockRule(
                wireMockConfig().fileSource(new ClasspathFileSource("path/to/wiremock/snipptes")).dynamicPort());

        @Test
        public void createSome() {
            ServiceClient.Some t = serviceClient.someOperation(new Some("some"));
            assertTrue(t.getId() > 0);
        }

//using dynamic ports requires to configure the ribbon server list accordingly
        @Profile("wiremock")
        @Configuration
        public static class TestConfiguration {

            @Bean
            public ServerList<Server> ribbonServerList() {
                return new StaticServerList<>(new Server("localhost", WIREMOCK.port()));
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)