Ale co miałoby usunąć ten obiekt jeśli nie używasz prawdziwego obiektu który zawiera te dane tylko mocka któremu zawsze kazałeś zwracać ten obiekt?
Możesz pokazać resztę testów które działają i implementację noteRepository i noteService?
Testy na dodawanie / modyfikację niby Ci działają?
@SpringBootTest
@AutoConfigureMockMvc
public class NoteServiceTest {
@Mock
private NoteRepository noteRepository;
@InjectMocks
private NoteServiceImpl noteService;
@Test
void shouldCreateNote_Test() {
Note note = new Note(1, LocalDate.now(), "testTitle", "testText");
when(noteRepository.save(note)).thenReturn(note);
Note resultNote = noteService.createNote(note);
assertEquals(note.getId(), resultNote.getId());
assertEquals("testTitle", resultNote.getTitle());
assertEquals("testText", resultNote.getText());
verify(noteRepository, times(1)).save(note);
}
@Test
void shouldFindNoteById_Test() {
Note note = new Note(1, LocalDate.now(), "testTitle", "testText");
when(noteRepository.findById(note.getId())).thenReturn(Optional.of(note));
Note resultNote = noteService.getNoteById(note.getId());
verify(noteRepository, times(1)).findById(note.getId());
assertEquals(note, resultNote);
}
@Test
void shouldFindAllNotes_Test() {
List<Note> mockNotes = Arrays.asList(new Note(1, LocalDate.now(), "testTitle", "testText")
, new Note(2, LocalDate.now(), "testTitle1", "testText1"));
when(noteRepository.findAll()).thenReturn(mockNotes);
List<Note> resultNotes = noteService.getAllNotes();
verify(noteRepository, times(1)).findAll();
assertIterableEquals(mockNotes, resultNotes);
}
@Test
void shouldUpdateNote_Test() {
Note existNote = new Note(1, LocalDate.now(), "testTitle", "testText");
Note updatetNote = new Note(1, LocalDate.now(), "testTitleUpdate", "testTextUpdate");
when(noteRepository.findById(existNote.getId())).thenReturn(Optional.of(existNote));
when(noteRepository.save(existNote)).thenReturn(updatetNote);
Note result = noteService.updateNote(existNote.getId(), updatetNote);
assertEquals(updatetNote.getTitle(), result.getTitle());
assertEquals(updatetNote.getText(), result.getText());
}
@Service
public class NoteServiceImpl implements NoteService {
private final NoteRepository noteRepository;
@Autowired
public NoteServiceImpl(NoteRepository noteRepository) {
this.noteRepository = noteRepository;
}
@Override
public Note createNote(Note note) {
note.setDate(LocalDate.now());
return noteRepository.save(note);
}
@Override
public List<Note> getAllNotes() {
List<Note> notes = noteRepository.findAll();
if (notes.isEmpty()) {
throw new EntityNotFoundException("List is empty");
}
return notes;
}
@Override
public Note getNoteById(Integer id) {
return noteRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Not found note"));
@Override
public void deleteAllNotes() {
getAllNotes();
noteRepository.deleteAll();
}
@Override
public void deleteById(Integer id) {
Note note = noteRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Note not found"));
noteRepository.delete(note);
}
@Override
public Note updateNote(Integer id, Note note) {
Note presentNote = getNoteById(id);
presentNote.setTitle(note.getTitle());
presentNote.setText(note.getText());
return noteRepository.save(presentNote);
}
}
Kod pewnie nie jest wybitny pewnie ale no uczę się.