title: Mocking an OpenFeign client for Unit Testing in a spring library and NOT for a spring boot application
body: <p>I've implemented a feign client that calls a get API based on this <a href="https://github.com/OpenFeign/feign" rel="nofollow noreferrer">official repository</a>. I have a rule class <code>UserValidationRule</code> that needs to call that get API call <code>getUser()</code> and validate some stuff. That works as expected but when I get to testing that rule class, mocking the feign client is not successful and it continues to call the actual API. I've simplified the situation so please ignore the simplicity lol. This is a follow up question I have after i found this <a href="https://stackoverflow.com/questions/56383675/using-feign-in-a-standalone-library-vs-a-springboot-application">stackoverflow</a> question</p>
  <p>The API returns this model:</p>
  <pre><code>@Data
  public class userModel {
      private long id;
      private String name;
      private int age;
  
  }
  </code></pre>
  <p>The interface with the rest client method:</p>
  <pre><code>public interface UserServiceClient {
  
      @RequestLine(&quot;GET /users/{id}&quot;)
      UserModel getUser(@Param(&quot;id&quot;) int id);
  }
  </code></pre>
  <p>And in the rule class, i build the feign client and call the API:</p>
  <pre><code>@RequiredArgsConstructor
  @Component
  public class UserValidationRule {
  
      private static final String API_PATH = &quot;http://localhost:8080&quot;;
  
      private UserServiceClient userServiceClient;
  
  
      public void validate(String userId, ...) {
              // some validations 
              validateUser(userId);
  
      }
  
      private void validateUser(String userId) {
              userServiceClient = getServiceClient();
              UserModel userModel = userServiceClient.gerUser(userId);
              
              // validate the user logic
          }
      }
  
      private UserServiceClient getServiceClient() {
             return Feign.builder()
                    .encoder(new GsonEncoder())
                    .decoder(new GsonDecoder())
                    .target(UserServiceClient.class, API_PATH);
      }
  }
  </code></pre>
  <p>And here comes the test class:</p>
  <pre><code>public class UserValidationRuleTest {
  
      private UserServiceClient userServiceClient = mock(UserServiceClient.class);
      private UserValidationRule validationRule = new UserValidationRule();
      private UserModel userModel;
  
      @Before
      public void init() {
          userModel = generateUserModel();
      }
  
  
      @Test
      public void validateWhenAgeIsNotBlank() {
  
          doReturn(userModel).when(userServiceClient).getUser(any());
          validationRule.validate(&quot;123&quot;, ...);
          // some logic ...
          assertEquals(.....);
          verify(userServiceClient).getUser(any());
      }
  
  
  
      private UserModel generateUserModel() {
          UserModel userModel = new UserModel();
          userModel.setName(&quot;Cody&quot;);
          userModel.setAge(&quot;22&quot;);
          return accountModel;
      }
  }
  </code></pre>
  <p>As I debug <code>validateWhenAgeIsNotBlank()</code>, i see that the userModel is not the one that's generated in the test class and the values are all null. If I pass in an actual <code>userId</code>, i get an actual UserModel that I have in my db.</p>
  <p>I think the problem is that <code>UserServiceClient</code> is not being mocked. The <code>verify</code> is failing as it says the <code>getUser()</code> is not invoked. It might be something to do with how the feign client is declared in the <code>UserValidationRule</code> with the feign.builder()...
  Please correct me if I'm wrong and tell me what I'm missing or any suggestions on how to mock it correctly.</p>