validate 설정
public record UserProfileRequest(
Long id,
@Size(min = 1, max = 10)
@NotBlank
String userName
) {
}
public record UserRequest(
@Size(min = 1, max = 10)
@NotBlank
String userName,
@Size(min = 1, max = 10)
@NotBlank
String userInfoId,
@Size(min = 8)
@NotBlank
@Pattern(regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$")
String userPassword,
@Email
String userEmail
) {
}
제한을 걸어두고, Controller에 @Valid 설정을 해둔다.
이걸 테스트코드로 작성해준다.
@Test
void 사용자_생성_아이디제한() {
RestAssured
.given().log().all()
.contentType(ContentType.JSON)
.body(new UserRequest("추민영", "", "abcABC123!!!", "chu@naver.com"))
.when()
.post("/users/infos")
.then().log().all()
.statusCode(400);
RestAssured
.given().log().all()
.contentType(ContentType.JSON)
.body(new UserRequest("추민영", "1234567891011", "abcABC123!!!", "chu@naver.com"))
.when()
.post("/users/infos")
.then().log().all()
.statusCode(400);
}
@Test
void 사용자_생성_비밀번호제한() {
RestAssured
.given().log().all()
.contentType(ContentType.JSON)
.body(new UserRequest("추민영", "chuchu", "abc", "chu@naver.com"))
.when()
.post("/users/infos")
.then().log().all()
.statusCode(400);
RestAssured
.given().log().all()
.contentType(ContentType.JSON)
.body(new UserRequest("추민영", "1234567891011", "abcABC", "chu@naver.com"))
.when()
.post("/users/infos")
.then().log().all()
.statusCode(400);
}
@Test
void 사용자_생성_이메일제한() {
RestAssured
.given().log().all()
.contentType(ContentType.JSON)
.body(new UserRequest("추민영", "chuchu", "abcABC!!!", "chunaver.com"))
.when()
.post("/users/infos")
.then().log().all()
.statusCode(400);
}
제한을 걸어둔게 제대로 실행하는지 테스트 해야한다. 그래야 실제로 동작할 때 제대로 돌아가는지 확인을 안해도 되기 때문이다.