### Question:
NoteService { @Transactional public Note updateNote(Note updatedNote, User user) { Note referencedNote = findById(updatedNote.getId()); referencedNote.setText(updatedNote.getText()); referencedNote = noteRepository.save(referencedNote); activityService.createActivity(user, referencedNote, Action.EDITED); return referencedNote; } @Autowired  NoteService(NoteRepository noteRepository, ActivityService activityService); Note findById(Long noteId); @Transactional Note updateNote(Note updatedNote, User user); @Transactional void deleteNote(Long noteId, User user);  }### Answer:
@Test public void updateNote_expectedUserNotifications() { when(noteRepository.findByIdOrThrow(originalId)).thenReturn(originalNote); noteService.updateNote(changedNote, authorizedUser); verify(activityService).createActivity(authorizedUser, null, Action.EDITED); }
@Test public void updateNote_expectedRepositorySave() { when(noteRepository.findByIdOrThrow(originalId)).thenReturn(originalNote); ArgumentCaptor<Note> capturedNotes = ArgumentCaptor.forClass(Note.class); noteService.updateNote(changedNote, authorizedUser); verify(noteRepository).save(capturedNotes.capture()); Note capturedNote = capturedNotes.getValue(); Assert.assertEquals(changedText, capturedNote.getText()); Assert.assertEquals(originalId, capturedNote.getId()); Assert.assertEquals(originalUserId, capturedNote.getUserId()); Assert.assertEquals(originalParentKeyResult, capturedNote.getParentKeyResult()); }
@Test public void updateNote_expectedCreatedActivity() { Note returnedNote = new Note(); when(noteRepository.findByIdOrThrow(originalId)).thenReturn(originalNote); when(noteRepository.save(any())).thenReturn(returnedNote); ArgumentCaptor<User> capturedUsers = ArgumentCaptor.forClass(User.class); ArgumentCaptor<Note> capturedNotes = ArgumentCaptor.forClass(Note.class); ArgumentCaptor<Action> capturedActions = ArgumentCaptor.forClass(Action.class); noteService.updateNote(changedNote, authorizedUser); verify(activityService) .createActivity( capturedUsers.capture(), capturedNotes.capture(), capturedActions.capture()); Assert.assertEquals(authorizedUser, capturedUsers.getValue()); Assert.assertEquals(returnedNote, capturedNotes.getValue()); Assert.assertEquals(Action.EDITED, capturedActions.getValue()); }
@Test public void updateNote_expectedCorrectReturn() { Note returnedNote = new Note(); when(noteRepository.findByIdOrThrow(originalId)).thenReturn(originalNote); when(noteRepository.save(any())).thenReturn(returnedNote); Note actualNote = noteService.updateNote(changedNote, authorizedUser); Assert.assertEquals(actualNote, returnedNote); }