From dc349a0c00361fdd9fd75e42f99a1a46f86df24d Mon Sep 17 00:00:00 2001 From: Azhwani Date: Sun, 14 Feb 2021 17:37:58 +0100 Subject: [PATCH] first commit --- .../NoConverterFoundApplication.java | 12 +++++ .../controller/StudentRestController.java | 21 ++++++++ .../boot/noconverterfound/model/Student.java | 53 +++++++++++++++++++ .../NoConverterFoundIntegrationTest.java | 50 +++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/NoConverterFoundApplication.java create mode 100644 spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/controller/StudentRestController.java create mode 100644 spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/model/Student.java create mode 100644 spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/noconverterfound/NoConverterFoundIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/NoConverterFoundApplication.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/NoConverterFoundApplication.java new file mode 100644 index 0000000000..7abfa29bf2 --- /dev/null +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/NoConverterFoundApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.boot.noconverterfound; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class NoConverterFoundApplication { + + public static void main(String[] args) { + SpringApplication.run(NoConverterFoundApplication.class, args); + } +} 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 new file mode 100644 index 0000000000..21cb98710d --- /dev/null +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/controller/StudentRestController.java @@ -0,0 +1,21 @@ +package com.baeldung.boot.noconverterfound.controller; + +import org.springframework.http.ResponseEntity; +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 com.baeldung.boot.noconverterfound.model.Student; + +@RestController +@RequestMapping(value = "/api") +public class StudentRestController { + + @GetMapping("/student/{id}") + public ResponseEntity get(@PathVariable("id") int id) { + // Custom logic + return ResponseEntity.ok(new Student(id, "John", "Wiliams", "AA")); + } + +} diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/model/Student.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/model/Student.java new file mode 100644 index 0000000000..94ece02f8f --- /dev/null +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/model/Student.java @@ -0,0 +1,53 @@ +package com.baeldung.boot.noconverterfound.model; + +public class Student { + + private int id; + private String firstName; + private String lastName; + private String grade; + + public Student() { + + } + + public Student(int id, String firstName, String lastName, String grade) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + this.grade = grade; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getGrade() { + return grade; + } + + public void setGrade(String grade) { + this.grade = grade; + } + +} 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 new file mode 100644 index 0000000000..3304a33957 --- /dev/null +++ b/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/noconverterfound/NoConverterFoundIntegrationTest.java @@ -0,0 +1,50 @@ +package com.baeldung.boot.noconverterfound; + +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 org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.converter.HttpMessageNotWritableException; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +import com.baeldung.boot.noconverterfound.controller.StudentRestController; + +@RunWith(SpringRunner.class) +@WebMvcTest(StudentRestController.class) +public class NoConverterFoundIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @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")); + + } + + @Test + public void whenGettersAreDefined_thenReturnObject() throws Exception { + + String url = "/api/student/2"; + + this.mockMvc.perform(get(url)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.firstName").value("John")); + + } + +}