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

Added tests for custom exceptions #556

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

YuriiSakharuk
Copy link

develop

GitHub Board

Issue link

Tests for custom exceptions #555

Code reviewers

Summary of issue

  1. Check uncovered custom exceptions;
  2. Cover custom exceptions with tests.

Summary of change

  1. Added 5 tests for custom exceptions;

Copy link
Collaborator

@V-Kaidash V-Kaidash left a comment

Choose a reason for hiding this comment

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

Please contact me any time if you need any help or if you have any questions.

UserEntity user = buildUserEntity(EMAIL, PASSWORD, true);
when(authenticationManager.authenticate(any(UsernamePasswordAuthenticationToken.class)))
.thenReturn(authentication);
when(tokenProvider.createToken(any(Authentication.class))).thenReturn(TOKEN);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please make assertion on token value after post request.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

}

private Authentication authenticate(LoginRequest loginRequest) {
return authenticationManager.authenticate(
Copy link
Collaborator

Choose a reason for hiding this comment

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

authenticationManager is mocked and couldn't provide proper response.
If I'm not mistaken, you can return
new UsernamePasswordAuthenticationToken(loginRequest.getEmail(), loginRequest.getPassword())
without calling authenticationManager

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

verify(postRepository, never())
.save(any(PostEntity.class)
);

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please remove empty line

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

Timestamp publishedAt = Timestamp.valueOf(
LocalDateTime.of(
LocalDate.of(
2002, Month.JANUARY, 14),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please move it on the same line with LocalDate.of(

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

@@ -37,6 +40,8 @@
@MockitoSettings(strictness = Strictness.LENIENT)
class AuthControllerTest {



Copy link
Collaborator

Choose a reason for hiding this comment

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

Please remove excessive empty lines

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

@@ -1278,6 +1279,79 @@ void updatePostById_WhenExists_isOk_DoctorRole() {
Assertions.assertThat(postService.updatePostById(userPrincipal, dto)).isTrue();
}

@Test
void updatePostById_WhenPostStatus_NotFount_ThrowException() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

NotFount it appears to be mistyping here. It should be NotFound

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
assertEquals(TOKEN, tokenProvider.createToken(authentication));
Copy link
Collaborator

Choose a reason for hiding this comment

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

That assertion doesn't make sense because it uses mocked value result from line 81 when(tokenProvider.createToken(authentication)).thenReturn(TOKEN);

You should compare TOKEN with value in the response after POST request. In your case, it will look like
mockMvc.perform(MockMvcRequestBuilders.post(URI) .content(asJsonString(loginRequest)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(mvcResult -> mvcResult.getResponse().getContentAsString().equals(TOKEN));

Copy link
Author

Choose a reason for hiding this comment

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

Fixed

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.

2 participants