Skip to content

Commit

Permalink
add exception throw in delete method of service and test it successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
nora-kauczor committed Oct 15, 2024
1 parent 8089938 commit 703591d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
3 changes: 3 additions & 0 deletions backend/src/main/java/org/example/backend/VocabService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.NoSuchElementException;

@RequiredArgsConstructor
@Service
Expand All @@ -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.";
}
Expand Down
16 changes: 9 additions & 7 deletions backend/src/test/java/org/example/backend/VocabServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}


}

0 comments on commit 703591d

Please sign in to comment.