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.
Asked
Active
Viewed 1.6k times
7

L42
- 3,052
- 4
- 28
- 49
1 Answers
6
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()));
}
}
}

Mathias Dpunkt
- 11,594
- 4
- 45
- 70
-
1Thank you for answering! I'm not using ribbon - only feign. I'm using the `@FeignClient` with `url = externalApiUrl`. How can I inject the wiremock url in there instead? – L42 Sep 21 '16 at 07:02
-
wiremock is actually running a server so just make sure that FeignClient is pointing to `localhost:WIREMOCK.port`. I am not sure if the url can be pointing to a configuration property. It does not seem to support spring expressions. In a similar question to have a dynamic feign url the accepted answer proposes to use ribbon and the config mechanism as used by the test above. http://stackoverflow.com/a/29278126/5371736 – Mathias Dpunkt Sep 21 '16 at 07:23
-
After a lot of struggling with spring versions, I think I'm closer. However, my autowired feign client gets set to `null`. Do you know why that could be? My current plan is to try to set the wiremock URL as a property in @SpringBootTest, so that the feign client (when not null) will go to wiremock. – L42 Sep 21 '16 at 13:27
-
I managed to get the autowiring to work. My next (and hopefully last) challenge is to get the feign client to use the wiremock url instead of the `${endpoint.service.url}` variable (set in application.yml) it usually goes to. I tried setting the property in the @SpringBootTest annotation, because quoting the docs it takes "Properties in form key=value that should be added to the Spring Environment before the test runs." But my guess is that my feign client is created before that happens. Do you have any suggestions? – L42 Sep 22 '16 at 07:56
-
After some good help from Dave Syer (founder of spring cloud) in the spring cloud gitter chat room, I found my mistake: Using an external config repo. It turns out that the external repo takes precedence, so my attempts at setting the url to something else never worked. – L42 Sep 22 '16 at 11:51
-
Thanks @MathiasDpunkt I didn't know the `WireMockRule.port()` method. This made my test pass. – Geekarist Feb 09 '17 at 11:41