Skip to content
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

Merged
merged 4 commits into from Nov 30, 2021
Merged

Add unit tests for Bean validation #22

merged 4 commits into from Nov 30, 2021

Conversation

kj84park
Copy link
Member

What is this PR for?

Overview or reasons

Copy link
Member

@seachicken seachicken left a 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.

Comment on lines 60 to 62
constraintViolations.forEach(
constraintViolation -> assertThat(constraintViolation.getMessage()).isEqualTo(MUST_NOT_BE_BLANK)
);
Copy link
Member

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.

Suggested change
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);
Copy link
Member

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.

Suggested change
assertThat(constraintViolations.size()).isEqualTo(3);
assertThat(constraintViolations).hasSize(3);

@BeforeEach
void setUp() throws IOException {
//Deep copy
registerCredential = objectMapper.readValue(objectMapper.writeValueAsString(origin), RegisterCredential.class);
Copy link
Member

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!

Comment on lines 41 to 48
@Test
void validateSuccessfulRequest() throws Exception {

mockMvc.perform(post(AUTH_CHALLENGE_URL_PATH)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(authChallengeRequest)))
.andExpect(status().isBadRequest());
}
Copy link
Member

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?

@kj84park
Copy link
Member Author

kj84park commented Nov 30, 2021

@seachicken
Thank you for taking the time to give us your suggestions! As you mentioned, I've made modifications!

@@ -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(
Copy link
Member

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

Copy link
Member Author

@kj84park kj84park Nov 30, 2021

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.

Copy link
Member

@seachicken seachicken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work 👍

@kj84park kj84park merged commit d4eefa3 into line:main Nov 30, 2021
@kj84park kj84park deleted the tc/add_test_cases branch November 30, 2021 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants