我目前正在提供覆盖 - 通过 MockMVC 请求调用测试我的 DTO 的验证。我最近在我的注册约束验证器中引入了一个新字段,supportedSpecializations,我从 application.properties 中注入了值,以便于维护和扩展。请参阅下面的代码片段:
@Component
public class RegistrationValidator implements ConstraintValidator<Registration, String> {
//campus.students.supportedspecializations="J2E,.NET,OracleDB,MySQL,Angular"
@Value("${campus.students.supportedspecializations}")
private String supportedSpecializations;
private String specializationExceptionMessage;
//All ExceptionMessages are maintained in a separate class
@Override
public void initialize(Registration constraintAnnotation) {
exceptionMessage = constraintAnnotation.regionException().getMessage();
}
@Override
public boolean isValid(RegistrationData regData, ConstraintValidatorContext context) {
String[] specializations = supportedSpecializations.split(",");
boolean isValidSpecialization = Arrays.stream(specializations)
.anyMatch(spec -> spec.equalsIgnoreCase(regData.getSpec()));
if (!isValidSpecialization){
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(specializationExceptionMessage)
.addConstraintViolation();
return false;
}
//additional validation logic...
return true;
}
}
由于 @Value 注释的定义属性未注入该字段,单元测试现在失败。我不确定ReflectionTestUtils是否对我有帮助,因此非常感谢有关如何在 UnitTests 中注入所需值的任何建议。
Spring 版本是 2.1.0 我目前正在使用以下代码段进行测试:
@InjectMocks
private StudentController mockRestController;
@Mock
private StudentService mockStudentService;
@Mock
private ValidationExceptionTranslator mockExceptionTranslator;
@Value("${campus.students.supportedspecializations}")
private String supportedSpecializations;
private MockMvc mockMvc;
private static final String VALIDATION_SUCCESSFUL = "success";
private static final String VALIDATION_FAILED = "failed";
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(mockRestController).build();
doReturn(
ResponseEntity.status(HttpStatus.OK)
.header("Content-Type", "text/html; charset=utf-8")
.body(VALIDATION_SUCCESSFUL))
.when(mockStudentService).insertStudent(Mockito.any());
doReturn(
ResponseEntity.status(HttpStatus.BAD_REQUEST)
.header("Content-Type", "application/json")
.body(VALIDATION_FAILED))
.when(mockExceptionTranslator).translate(Mockito.any());
}
@Test
public void testValidation_UnsupportedSpecialization() throws Exception {
MvcResult mvcResult = mockMvc.perform(
post("/Students").contentType(MediaType.APPLICATION_JSON_UTF8).content(
"{\"registrationData\":{\"spec\":\"unsupported\"}}"))
.andExpect(status().isBadRequest())
.andReturn();
assertEquals(VALIDATION_FAILED, mvcResult.getResponse().getContentAsString());
verify(mockExceptionTranslator, times(1)).translate(Mockito.any());
verify(mockStudentService, times(0)).insertStudent(Mockito.any());
}
我尝试使用@RunWith(SpringRunner.class)和@SpringBootTest(classes = Application.class)注释我的 Test 类,但由于 @Value 未解决,验证测试仍然失败。我可能错了,但我认为 ConstraintValidator 的实例是在我们到达 restController 之前创建的,所以 MockMVC perform(...)调用不能简单地确保验证器中的适当 @Value 被注入到 supportedSpecializations 中。