first commit
This commit is contained in:
parent
030b5cca02
commit
dc349a0c00
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<Student> get(@PathVariable("id") int id) {
|
||||
// Custom logic
|
||||
return ResponseEntity.ok(new Student(id, "John", "Wiliams", "AA"));
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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"));
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue