diff --git a/backend/src/main/java/org/example/backend/VocabService.java b/backend/src/main/java/org/example/backend/VocabService.java index 16abd003..debb4fad 100644 --- a/backend/src/main/java/org/example/backend/VocabService.java +++ b/backend/src/main/java/org/example/backend/VocabService.java @@ -4,6 +4,7 @@ import org.springframework.stereotype.Service; import java.util.List; +import java.util.NoSuchElementException; @RequiredArgsConstructor @Service @@ -19,6 +20,8 @@ public Vocab getVocab(String id){ } public String deleteVocab(String id){ + if ( !vocabRepo.existsById(id)) {throw new NoSuchElementException(); + } vocabRepo.deleteById(id); return "Vocab successfully deleted."; } diff --git a/backend/src/test/java/org/example/backend/VocabServiceTest.java b/backend/src/test/java/org/example/backend/VocabServiceTest.java index 61cb73b8..62d68cd3 100644 --- a/backend/src/test/java/org/example/backend/VocabServiceTest.java +++ b/backend/src/test/java/org/example/backend/VocabServiceTest.java @@ -46,22 +46,24 @@ void getVocab_shouldThrowNoSuchElementException_whenCalledWithNonexistentId() { @Test void deleteVocab_shouldTriggerDeletionOfVocab_whenCalledWithId(){ - Vocab testVocab = new Vocab("000", "la prueba", "test", - "", "Spanish", List.of()); - mockVocabRepo.save(testVocab); + when(mockVocabRepo.existsById("000")).thenReturn(true); vocabService.deleteVocab("000"); - assertFalse(mockVocabRepo.existsById("000")); verify(mockVocabRepo).deleteById("000"); } @Test void deleteVocab_shouldReturnString_whenCalledWithId(){ - Vocab testVocab = new Vocab("000", "la prueba", "test", - "", "Spanish", List.of()); - mockVocabRepo.save(testVocab); + when(mockVocabRepo.existsById("000")).thenReturn(true); String expected = "Vocab successfully deleted."; String actual = vocabService.deleteVocab("000"); assertEquals(expected, actual); } + @Test + void deleteVocab_shouldThrowNoSuchElementException_whenCalledWithNonexistentId(){ + assertThrows(NoSuchElementException.class, () -> vocabService.deleteVocab("000")); + verify(mockVocabRepo).existsById("000"); + } + + } \ No newline at end of file