From 64017bca19ed9bb2fe756404063559e8185b93dc Mon Sep 17 00:00:00 2001 From: Nora Kauczor Date: Tue, 15 Oct 2024 09:56:48 +0200 Subject: [PATCH] write exception handler in controller and test it successfully --- .../main/java/org/example/backend/ErrorMessage.java | 4 ++++ .../java/org/example/backend/VocabController.java | 13 +++++++++---- .../org/example/backend/VocabControllerTest.java | 9 +++++++++ .../java/org/example/backend/VocabServiceTest.java | 2 +- 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 backend/src/main/java/org/example/backend/ErrorMessage.java diff --git a/backend/src/main/java/org/example/backend/ErrorMessage.java b/backend/src/main/java/org/example/backend/ErrorMessage.java new file mode 100644 index 00000000..7caed0b6 --- /dev/null +++ b/backend/src/main/java/org/example/backend/ErrorMessage.java @@ -0,0 +1,4 @@ +package org.example.backend; + +public record ErrorMessage(String message) { +} diff --git a/backend/src/main/java/org/example/backend/VocabController.java b/backend/src/main/java/org/example/backend/VocabController.java index 923bcbed..69fa6a2b 100644 --- a/backend/src/main/java/org/example/backend/VocabController.java +++ b/backend/src/main/java/org/example/backend/VocabController.java @@ -1,12 +1,11 @@ package org.example.backend; import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.NoSuchElementException; @RestController @RequestMapping("/api/vocab") @@ -24,4 +23,10 @@ public List getAllVocabs(){ public Vocab getVocab(@PathVariable String _id){ return vocabService.getVocab(_id); } + + @ResponseStatus(HttpStatus.NOT_FOUND) + @ExceptionHandler(NoSuchElementException.class) + public ErrorMessage handleElementNotFoundException(){ + return new ErrorMessage("No matches for given ID."); + } } diff --git a/backend/src/test/java/org/example/backend/VocabControllerTest.java b/backend/src/test/java/org/example/backend/VocabControllerTest.java index bd7768fc..ce8fe5b5 100644 --- a/backend/src/test/java/org/example/backend/VocabControllerTest.java +++ b/backend/src/test/java/org/example/backend/VocabControllerTest.java @@ -52,4 +52,13 @@ void getVocab_shouldReturnSpecificVocab_whenCalledWithItsId() throws Exception { "info":"", "language":"Spanish", "reviewDates":[]} """)); } + + @DirtiesContext + @Test + void getVocab_ShouldTriggerNotFoundStatus_whenCalledWithNonexistentID() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/api/vocab/000")) + .andExpect(status().isNotFound()); + } + + } \ No newline at end of file diff --git a/backend/src/test/java/org/example/backend/VocabServiceTest.java b/backend/src/test/java/org/example/backend/VocabServiceTest.java index 6ab63b12..1a7e56da 100644 --- a/backend/src/test/java/org/example/backend/VocabServiceTest.java +++ b/backend/src/test/java/org/example/backend/VocabServiceTest.java @@ -37,7 +37,7 @@ void getVocab_shouldReturnSpecificVocab_whenCalledWithItsId() { } @Test - void getVocab_shouldThrowNoSuchElementException_whenCalledWithNonexistantId() { + void getVocab_shouldThrowNoSuchElementException_whenCalledWithNonexistentId() { when(mockVocabRepo.findById("000")).thenThrow(NoSuchElementException.class); assertThrows(NoSuchElementException.class, () -> mockVocabRepo.findById("000")); verify(mockVocabRepo).findById("000");