简体   繁体   English

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

[英]How to use WireMock on a Feign client in a Spring Boot application?

I have a class that makes use of a Feign client. 我有一个使用Feign客户端的课程。 Previously I used Mockito and gave a stored response for each of the method calls in the Feign client. 之前我使用过Mockito并为Feign客户端中的每个方法调用提供了存储响应。 Now I want to use WireMock, so that I can see that my code handles different kinds of response codes correctly. 现在我想使用WireMock,以便我可以看到我的代码正确处理不同类型的响应代码。 How do I go about doing this? 我该怎么做呢? I can't figure out how to wire up my Feign client in the test, and wire it up so that it uses Wiremock instead of the URL I've setup in my application.yml file. 我无法弄清楚如何在测试中连接我的Feign客户端,并将其连接起来,以便它使用Wiremock而不是我在application.yml文件中设置的URL。 Any pointers would be greatly appreciated. 任何指针都将非常感激。

Maybe you want to look at this project https://github.com/ePages-de/restdocs-wiremock 也许你想看看这个项目https://github.com/ePages-de/restdocs-wiremock

This helps you generate and publish wiremock snippets in your spring mvc tests (using spring-rest-docs). 这有助于您在spring mvc测试中生成和发布wiremock片段(使用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. 如果您回避这个集成解决方案,您可以在测试期间使用线缆模拟JUnit规则来启动线程服务器。 http://wiremock.org/docs/junit-rule/ 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()));
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Spring Boot feign 客户端进行 Oauth2 身份验证? - How to use Spring Boot feign client for Oauth2 authentication? Feign客户端,Spring Boot应用程序和rx / Observable类未找到错误 - Feign Client, Spring Boot Application, and rx/Observable Class Not Found Error Spring Boot hatoas + swagger +feign 客户端错误应用启动 - Spring Boot hateoas + swagger +feign client error application start up 如何使用 postman 或 feign 客户端在 Spring 引导中将值设置为 @RequestAttribute - How to set value to @RequestAttribute in Spring boot using postman or feign client 如何在 spring 启动应用程序状态之前运行wiremock? - How to get wiremock running before the spring boot application status up? 将自定义的伪装客户端注入Spring应用程序 - Inject custom feign client into Spring Application 如何从Spring Boot Feign客户端登录到远程Web服务 - How can I login to remote webservice from spring boot feign client 如何使用 Feign Client Spring Boot 从另一个服务获取数据(错误:406) - How to get data from another service using Feign Client Spring Boot (ERROR: 406) 如何在 spring 引导 Java 中动态获取假客户端名称和 url - how to get feign client name and url both dynamically in spring boot Java Spring Boot假装异常 - Spring Boot feign exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM