17

i am using spring cloud's eureka and feign to communicate between some services (lets say A and B). Now id like to unittest my service layer of a single service (A). The problem is, that this service (A) is using a feign client to request some information of the other service (B).

Running the unittests without any special configuration throws the following exception: java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: service-b => but i do not want any server to run.

My question is: Is there a way to mock the feign client, so i can unittest my service (A) without running an eureka instance and service (B)?

Edit: I ended up creating a stub for the feign client. The stub is marked as a primary component to force spring instantiating the stub within my tests.
This is the solution i came up with.

//the feign client
@FeignClient("user") 
public interface UserClient { 
    UserEntity getUser(); 
}

//the implementation i use for the tests 
@Component 
@Primary //mark as primary implementation
public class UserClientTestImpl implements UserClient { 
    @Override public UserEntity getUser() { 
        return someKindOfUser; 
    } 
}

3 Answers 3

9

The question is ... do you even need to mock? I often see that people mention "mock" as the first solution to anything that "should not be part of the unit test". Mocking is a technique, not the solution to everything. (see here).

If you are still at the early stages of your code, just refactor and use something else instead of depending on the concrete instance of the Feign Client. You might use an interface, an abstract class, a trait or whatever you want. Don't depend on the object itself, otherwise you have to "mock it".

public interface IWebClient {
  public String get(...);
  public String post(...);
} 

To the question: but I will have other code that will do exactly the same (except that it will be on the concrete instance of Feign), what do I do then? Well, you can write a functional test and call an instance of a web server that you can setup locally - or use Wiremock, as mentioned by Marcin Grzejszczak in one of the answers.

public class FeignClientWrapper implements IWebClient {
  private feign = something

  public String get() {
    feign.get( ... ) 
  }

  public String post() {
    feign.post( ... ) 
  }
} 

Unit tests are used to test algorithms, if/else, loops: how units work. Don't write code to make mocks fit - it must be the other way around: your code should have less dependencies, and you should mock only when you need to verify the behavior (otherwise you can use a stub or a fake object): do you need to verify the behavior? Do you need to test that a particular method gets called in your code? Or that a particular method gets called with X, Y, and Z for 3 times in a row? Well, then yes, mocking is ok.

Otherwise, use a fake object: what you want is to test just the call/response and maybe the status code. All you probably want is to test how your code reacts to different outputs (e.g., the field "error" is present or not in a JSON response), different status codes (assuming that the Client documentation is right: 200 OK when GET, 201 when POST, etc).

7
  • This is the solution i came up with so far: The feign interface @FeignClient("user") public interface UserClient { //some feign annotations UserEntity getUser(); } The implementation i use for the tests @Component @Primary public class UserClientTestImpl implements UserClient { @Override public UserEntity getUser() { return someKindOfUser; } } Basically its the method you have mentioned @Markon. Commented Dec 22, 2015 at 15:29
  • 1
    Could you maybe post the code in your question? It's hard to read code in the comments :D I am happy it helped. When you want to test the behavior, then you can use mocking. If you want to test the "connection", setup a small webserver! :P
    – Markon
    Commented Dec 22, 2015 at 16:19
  • I need to mock responses of a feignClient so that I do not start other microservice. What is wrong with it? Also I do not want to use external additional libraries for such a simple case.
    – Kirill
    Commented Aug 2, 2019 at 14:13
  • @KirillCh if you depend on the object itself, you need to mock it. If you depend on the interface, then it becomes trivial. In that case you won't need any external library, not even mockito! And as usual, it depends on what you want to test; with mocks you want to test the behavior.
    – Markon
    Commented Aug 2, 2019 at 14:34
  • Thank you. May I ask an official question and give you a link below, because it is very important to me but I do not understand you. Ok?
    – Kirill
    Commented Aug 2, 2019 at 14:36
9

Mocking a feign client is really useful in microservice component tests. You want to test one microservice without having to start all the other microservices.

If you're using Spring (and it looks like you are), the @MockBean annotation together with a bit of Mockito code will do the job.

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = 
SpringBootTest.WebEnvironment.DEFINED_PORT)
public class TestYourComponent {
    @Configuration
    @Import({YourConfiguration.class})
    public static class TestConfiguration {
    }

    @MockBean
    private UserClient userClient;

    @Test
    public void someTest()
    {
        //...
        mockSomeBehavior();
        //...
    }

    private void mockSomeBehavior() {
        Mockito.doReturn(someKindOfUser).when(userClient).getUser();
    }
}
3
  • Typically, this is not what a unit test should do. You can really see this by even looking at the dependencies for your tests: SpringBootTest should ring a bell here, because you are initializing the entire springboot context (+ all the beans, etc.). What do you need that for in a Unit Test? This is more about integration/components testing" than unit tests.
    – Markon
    Commented Dec 13, 2018 at 15:31
  • 1
    @Markon, if you read what I wrote, you'll see that I'm talking about a component test, not a unit test. Perhaps you meant to respond to the original question, which did claim to be about unit testing? ...although, it's pretty clear that the original questioner means component testing too even though they use the wrong word. Commented Dec 14, 2018 at 16:12
  • yes, I know it's about component testing. I just wanted to add something to your answer, because I see more and more confusion from people calling components tests (like that one you wrote) unit tests. And I also know that you know the difference :)
    – Markon
    Commented Dec 14, 2018 at 16:30
4

If you need to use a mock you can use Wiremock to stub the response for a given request - http://wiremock.org/stubbing.html. That way you will do integration tests with real HTTP requests sent. For unit testing the answer from @Markon is very good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.