[英]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.