컨트롤러에서 연락처 포맷(000-0000-0000)에 맞는 필드를 받아보자
CellPhoneNumber : Valid용 Annotation
CellPhoneNumberValidator : 실제 검증하는 로직이 있는 Validator
SampleDto : Request에서 연락처를 받을 DTO 클래스
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = CellPhoneNumberValidator.class)
public @interface CellPhoneNumber {
String message() default "Invalid cellPhone number";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
public class CellPhoneNumberValidator implements ConstraintValidator<CellPhoneNumber, String> {
@Override
public void initialize(CellPhoneNumber constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (!StringUtils.hasText(value)) {
return false;
}
Pattern pattern = Pattern.compile("\\d{3}-\\d{4}-\\d{4}");
Matcher matcher = pattern.matcher(value);
return matcher.matches();
}
}
@Getter
@Setter
public class SampleDto {
@CellPhoneNumber
private String cellPhoneNumber;
}
in SampleController
@GetMapping("/sample")
public void testSampleDto(@Valid SampleDto sampleDto) {
...
}
in SampleControllerTest
@Test
void testReturnBadRequest() throws Exception {
mockMvc.perform(get("/sample")
.param("cellPhoneNumber", "02-111-1241")
)
.andDo(print())
.andExpect(status().isBadRequest());
}
@Test
void testOk() throws Exception {
mockMvc.perform(get("/sample")
.param("cellPhoneNumber", "010-1113-1245")
)
.andDo(print())
.andExpect(status().isOk());
}