From 61dd13d64e02b3ef8434dd3faefa87014ad69b5f Mon Sep 17 00:00:00 2001 From: Azhwani <13301425+azhwani@users.noreply.github.com> Date: Tue, 8 Feb 2022 15:48:06 +0100 Subject: [PATCH] init commit (#11767) --- .../controller/StudentRestController.java | 11 +++++++ .../NoConverterFoundIntegrationTest.java | 30 +++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/controller/StudentRestController.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/controller/StudentRestController.java index 21cb98710d..4119b624e1 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/controller/StudentRestController.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/controller/StudentRestController.java @@ -1,5 +1,6 @@ package com.baeldung.boot.noconverterfound.controller; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -18,4 +19,14 @@ public class StudentRestController { return ResponseEntity.ok(new Student(id, "John", "Wiliams", "AA")); } + @GetMapping(value = "/student/v2/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity getV2(@PathVariable("id") int id) { + return ResponseEntity.ok(new Student(id, "Kevin", "Cruyff", "AA")); + } + + @GetMapping(value = "/student/v3/{id}", produces = MediaType.APPLICATION_XML_VALUE) + public ResponseEntity getV3(@PathVariable("id") int id) { + return ResponseEntity.ok(new Student(id, "Robert", "Miller", "BB")); + } + } diff --git a/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/noconverterfound/NoConverterFoundIntegrationTest.java b/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/noconverterfound/NoConverterFoundIntegrationTest.java index f8ded91e65..6f89ef0e58 100644 --- a/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/noconverterfound/NoConverterFoundIntegrationTest.java +++ b/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/noconverterfound/NoConverterFoundIntegrationTest.java @@ -4,6 +4,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import org.junit.Test; import org.junit.runner.RunWith; @@ -25,16 +26,16 @@ public class NoConverterFoundIntegrationTest { /* Remove Getters from Student class to successfully run this test case * @Test public void whenGettersNotDefined_thenThrowException() throws Exception { - + String url = "/api/student/1"; - + this.mockMvc.perform(get(url)) .andExpect(status().isInternalServerError()) .andExpect(result -> assertThat(result.getResolvedException()) .isInstanceOf(HttpMessageNotWritableException.class)) .andExpect(result -> assertThat(result.getResolvedException().getMessage()) .contains("No converter found for return value of type")); - + } */ @@ -44,9 +45,28 @@ public class NoConverterFoundIntegrationTest { String url = "/api/student/2"; this.mockMvc.perform(get(url)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.firstName").value("John")); + .andExpect(status().isOk()) + .andExpect(jsonPath("$.firstName").value("John")); + } + @Test + public void whenJsonConverterIsFound_thenReturnResponse() throws Exception { + String url = "/api/student/v2/1"; + + this.mockMvc.perform(get(url)) + .andExpect(status().isOk()) + .andExpect(content().json("{'id':1,'firstName':'Kevin','lastName':'Cruyff', 'grade':'AA'}")); + } + + @Test + public void whenConverterNotFound_thenThrowException() throws Exception { + String url = "/api/student/v3/1"; + + this.mockMvc.perform(get(url)) + .andExpect(status().isInternalServerError()) + .andExpect(result -> assertThat(result.getResolvedException()).isInstanceOf(HttpMessageNotWritableException.class)) + .andExpect(result -> assertThat(result.getResolvedException() + .getMessage()).contains("No converter for [class com.baeldung.boot.noconverterfound.model.Student] with preset Content-Type")); } }