Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 378
L42 Avatar asked Sep 19 '16 14:09

L42


People also ask

What is the use of WireMock in spring boot?

WireMock is a library for stubbing and mocking web services. It constructs an HTTP server that acts as an actual web service. In simple terms, it is a mock server that is highly configurable and has the ability to return recorded HTTP responses for requests matching criteria.

How do you communicate between Microservices using Feign client?

Let's implement the Feign in our project and invoke other microservices using Feign. Step 1: Select currency-conversion-service project. Step 2: Open the pom. xml and add the Feign dependency.

How do you write a unit test case for feign client?

Create a Unit Test ClassCreate a MessagingRestClientBootTests class with @SpringBootTest annotation using a random port web environment. @SpringBootTest will create an about complete application context for MessagingRestClientBootTests containing all the objects we need to run our unit test.


1 Answers

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()));
            }
        }
    }
like image 180
Mathias Dpunkt Avatar answered Sep 16 '22 17:09

Mathias Dpunkt