【问题标题】:Full validation test in Spring Boot, injection failingSpring Boot中的完整验证测试,注入失败
【发布时间】:2018-01-10 13:30:43
【问题描述】:

大家好,我想在我的 Spring Boot 应用程序中测试请求的完整验证,我的意思是一次不测试一个验证器,但所有验证器都在目标对象上)

首先我有我的对象:

public class UserCreationRequest {
   @JsonProperty("profileId")
   @NotNull
   @ValidProfile
   private Integer profileId; 
}

然后是我的验证器(@ValidProfile):

@Component
public class ProfileValidator implements ConstraintValidator<ValidProfile,   Integer> {

@Autowired
private IProfileService profileService;

@Autowired
private IUserRestService userRestService;

@Override
public void initialize(ValidProfile constraintAnnotation) {

}

@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
    if (value == null) {
        return true;
    }

    RestUser restUser = userRestService.getRestUser();
    ProfileEntity profileEntity = profileService.getProfile(value, restUser.getAccountId());

    return profileEntity != null;
}
}

现在我编写单元测试:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {ValidationTestConfiguration.class})
public class UserCreationRequestValidationTest {

private static LocalValidatorFactoryBean localValidatorFactory;

@Autowired
private IUserService userService;

@Autowired
private IProfileService profileService;

@Autowired
private IUserRestService restService;

@BeforeClass
public static void createValidator() {
    localValidatorFactory = new LocalValidatorFactoryBean();
    localValidatorFactory.setProviderClass(HibernateValidator.class);
    localValidatorFactory.afterPropertiesSet();
}

@AfterClass
public static void close() {
    localValidatorFactory.close();
}    

@Test
public void validateUserCreationRequestStringfields() {

    UserCreationRequest userCreationRequest = new UserCreationRequest();
    /* Here fill test object*/

    when(userService.getUser(any(Integer.class), any(Integer.class))).thenReturn(new UserEntity());
    when(profileService.getProfile(any(Integer.class), any(Integer.class))).thenReturn(new ProfileEntity());
    when(restService.getRestUser()).thenReturn(new RestUser());

    Set<ConstraintViolation<UserCreationRequest>> violations
            = localValidatorFactory.validate(userCreationRequest);

    assertEquals(violations.size(), 8);
}
}

我的 TestConfiguration 就是这样:

@Configuration
public class ValidationTestConfiguration {

    @Bean
    @Primary
    public IProfileService profileService() {
        return Mockito.mock(IProfileService.class);
    }   

    @Bean
    @Primary
    public IUserRestService userRestService() { return Mockito.mock(IUserRestService.class); }
}

在执行时,我可以看到在测试本身中注入有效: restService 映射到“Mock for IUserRestService”

但在我的验证器中它没有被注入,userRestService 为空。

ProfileService 也是如此

我尝试了这里看到的几件事,没有任何效果(代码正在运行,只有测试配置失败)

【问题讨论】:

    标签: java unit-testing validation spring-boot


    【解决方案1】:

    这是因为你没有生成 Validator bean,所以它可以被注入。

    当您手动实例化 LocalValidatorFactoryBean 时,它无法访问为此测试定义的 spring DI。

    您应该为 Validator 生成一个 bean,或者甚至引用现有的 spring 配置来这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-29
      • 2020-05-08
      • 2015-08-25
      • 1970-01-01
      • 2015-08-09
      • 2021-06-24
      • 1970-01-01
      • 2012-10-17
      相关资源
      最近更新 更多