模拟 OpenFeign 客户端以在 spring 库中进行单元测试而不是 spring 启动应用程序

Mocking an OpenFeign client for Unit Testing in a spring library and NOT for a spring boot application

我实现了一个假客户端,它根据这个 official repository. I have a rule class UserValidationRule that needs to call that get API call getUser() 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 Whosebug 问题

调用 get API

APIreturns本机型:

@Data
public class userModel {
    private long id;
    private String name;
    private int age;

}

与其余客户端方法的接口:

public interface UserServiceClient {

    @RequestLine("GET /users/{id}")
    UserModel getUser(@Param("id") int id);
}

并且在规则 class 中,我构建了假客户端并调用了 API:

@RequiredArgsConstructor
@Component
public class UserValidationRule {

    private static final String API_PATH = "http://localhost:8080";

    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);
    }
}

测试来了class:

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("123", ...);
        // some logic ...
        assertEquals(.....);
        verify(userServiceClient).getUser(any());
    }



    private UserModel generateUserModel() {
        UserModel userModel = new UserModel();
        userModel.setName("Cody");
        userModel.setAge("22");
        return accountModel;
    }
}

当我调试 validateWhenAgeIsNotBlank() 时,我发现 userModel 不是在测试 class 中生成的那个,并且值都是空的。如果我传入实际的 userId,我会得到我的数据库中的实际 UserModel。

我认为问题在于 UserServiceClient 没有被嘲笑。 verify 失败,因为它表示 getUser() 未被调用。这可能与在 UserValidationRule 中使用 feign.builder() 声明伪装客户端的方式有关... 如果我错了请纠正我并告诉我我遗漏了什么或关于如何正确模拟它的任何建议。

您没有使用 spring 托管 UserServiceClient bean。每次调用 UserValidationRule.validate 时,它都会调用 validateUser,后者又会调用 getServiceClient 方法。 getServiceClient 为每次调用创建一个新的 UserServiceClient 实例。这意味着在测试模拟时 UserServiceClient 根本没有被使用。

我将重组代码如下;

首先用 @RequiredArgsConstructor 声明 UserServiceClient 为最终版本,或者用 @AllArgsConstructor 替换 @RequiredArgsConstructor。此更改的目的是允许注入 UserServiceClient 的实例,而不是在服务方法内部创建。

@Component
@RequiredArgsConstructor
public class UserValidationRule {

    private final UserServiceClient userServiceClient;

.... // service methods

}

然后有一个单独的 Configuration class 将 feign 客户端构建为 spring bean;

@Bean
private UserServiceClient userServiceClient() {
       return Feign.builder()
              .encoder(new GsonEncoder())
              .decoder(new GsonDecoder())
              .target(UserServiceClient.class, API_PATH);
}

在运行时,这个 bean 现在将被注入 UserValidationRule

至于单元测试更改,您正在正确创建模拟但不是 setting/injecting 任何地方的模拟。您要么需要使用 @Mock@InjectMocks 注释,要么在 @Before 方法中手动创建 UserValidationRule 的实例。

下面是 @Mock@InjectMocks 的用法;

@RunWith(MockitoJUnitRunner.class)
public class UserValidationRuleTest {

    @Mock private UserServiceClient userServiceClient;
    @InjectMocks private UserValidationRule validationRule;
    ... rest of the code

或继续使用mock(...)方法并手动创建UserValidationRule.

public class UserValidationRuleTest {

    private UserServiceClient userServiceClient = mock(UserServiceClient.class);
    private UserValidationRule validationRule;
    private UserModel userModel;

    @Before
    public void init() {
        validationRule = new UserValidationRule(userServiceClient);
        userModel = generateUserModel();
    }
    ... rest of the code

现在这将确保您在运行时使用 spring 托管假客户端 bean 的单个实例,并使用模拟实例进行测试。