How to use WireMock on a Feign client in a Spring Boot application?
I have a class that makes use of a Feign client. Previously I used Mockito and gave a stored response for each of the method calls in the Feign client. Now I want to use WireMock, so that I can see that my code handles different kinds of response codes correctly. 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. Any pointers would be greatly appreciated.
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()));
}
}
}
链接地址: http://www.djcxy.com/p/93178.html