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

我有一个使用Feign客户端的类。 之前我使用Mockito并为Feign客户端中的每个方法调用给出了一个存储响应。 现在我想使用WireMock,以便我可以看到我的代码正确处理了不同类型的响应代码。 我如何去做这件事? 我无法弄清楚如何在测试中连接我的Feign客户端,并将它连接起来,以便它使用Wiremock而不是我在我的application.yml文件中设置的URL。 任何指针将不胜感激。


也许你想看看这个项目https://github.com/ePages-de/restdocs-wiremock

这有助于您在spring mvc测试中使用spring-rest-docs生成和发布WireMock片段。

最后,您可以使用这些代码段来启动一个连线服务器,以便在测试中提供这些记录的请求。

如果您回避这种集成解决方案,您可以使用Wiremock JUnit规则在测试过程中启动您的电缆连接服务器。 http://wiremock.org/docs/junit-rule/

以下是使用动态连线模式端口并配置功能区以使用此端口的示例测试:(您是否使用假名和功能区?)

    @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()));
            }
        }
    }
链接地址: http://www.djcxy.com/p/93177.html

上一篇: How to use WireMock on a Feign client in a Spring Boot application?

下一篇: RelativeLayout alignParentBottom conflict with android