New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add unit tests for Bean validation #22
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job! I'll be relieved when the test is added.
constraintViolations.forEach( | ||
constraintViolation -> assertThat(constraintViolation.getMessage()).isEqualTo(MUST_NOT_BE_BLANK) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A little easier to read.
constraintViolations.forEach( | |
constraintViolation -> assertThat(constraintViolation.getMessage()).isEqualTo(MUST_NOT_BE_BLANK) | |
); | |
assertThat(constraintViolations).extracting(ConstraintViolation::getMessage) | |
.containsOnly(MUST_NOT_BE_BLANK); |
|
||
final Set<ConstraintViolation<RegisterCredential>> constraintViolations = validator.validate(registerCredential); | ||
|
||
assertThat(constraintViolations.size()).isEqualTo(3); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the AssertJ feature may make it a bit more readable.
assertThat(constraintViolations.size()).isEqualTo(3); | |
assertThat(constraintViolations).hasSize(3); |
@BeforeEach | ||
void setUp() throws IOException { | ||
//Deep copy | ||
registerCredential = objectMapper.readValue(objectMapper.writeValueAsString(origin), RegisterCredential.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice idea to load the base request first!
@Test | ||
void validateSuccessfulRequest() throws Exception { | ||
|
||
mockMvc.perform(post(AUTH_CHALLENGE_URL_PATH) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(authChallengeRequest))) | ||
.andExpect(status().isBadRequest()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the production code and the test code are packaged differently, we may not notice that there are tests.
How about writing them in ChallengeControllerTest
as the same web layer test?
@seachicken |
@@ -40,9 +40,10 @@ void validateIncompleteRequestWithBlank() { | |||
|
|||
final Set<ConstraintViolation<AuthOptionRequest>> constraintViolations = validator.validate(authOptionRequest); | |||
|
|||
assertThat(constraintViolations.size()).isEqualTo(1); | |||
assertThat(constraintViolations).hasSize(1); | |||
constraintViolations.forEach( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
containsOnly checks all the elements of a collection, so we can get rid of forEach
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, thanks! I fixed it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work 👍
What is this PR for?
Overview or reasons
Improve message validation check #20