Skip to content

Commit

Permalink
add tests resume controller
Browse files Browse the repository at this point in the history
  • Loading branch information
cankurttekin committed Nov 22, 2024
1 parent 67c1cc7 commit 810bd1f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ResponseEntity<ResumeDTO> createOrUpdateResume(

return ResponseEntity.status(HttpStatus.CREATED).body(createdOrUpdatedResume);
}

@GetMapping
public ResponseEntity<ResumeDTO> getResume(
@AuthenticationPrincipal UserDetails userDetails) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,51 @@ public void testCreateOrUpdateResume_Success() {
verify(userService).findUserByUsername(user.getUsername());
verify(resumeService).createOrUpdateResume(resume);
}

@Test
public void testCreateOrUpdateResume_UserNotFound() {
// Mock userService behavior to return empty
when(userService.findUserByUsername(user.getUsername())).thenReturn(Optional.empty());

// Expect an exception when calling the method
RuntimeException exception = assertThrows(RuntimeException.class,
() -> resumeController.createOrUpdateResume(resume, userDetails));

assertEquals("User not found", exception.getMessage());
verify(userService).findUserByUsername(user.getUsername());
verifyNoInteractions(resumeService); // Ensure resumeService is not called
}

@Test
public void testGetResume_Success() {
// Mock userService behavior
when(userService.findUserByUsername(user.getUsername())).thenReturn(Optional.of(user));

// Mock resumeService behavior
when(resumeService.findById(user.getId())).thenReturn(resumeDTO);

// Call the controller method
ResponseEntity<ResumeDTO> response = resumeController.getResume(userDetails);

// Verify and assert
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(resumeDTO, response.getBody());
verify(userService).findUserByUsername(user.getUsername());
verify(resumeService).findById(user.getId());
}

@Test
public void testGetResume_UserNotFound() {
// Mock userService behavior to return empty
when(userService.findUserByUsername(user.getUsername())).thenReturn(Optional.empty());

// Expect an exception when calling the method
RuntimeException exception = assertThrows(RuntimeException.class,
() -> resumeController.getResume(userDetails));

assertEquals("User not found", exception.getMessage());
verify(userService).findUserByUsername(user.getUsername());
verifyNoInteractions(resumeService); // Ensure resumeService is not called
}

}

0 comments on commit 810bd1f

Please sign in to comment.