* Moved Controller vs RestController article code from spring-mvc-java to spring-mvc-basics

* now using jupiter tests for the tests involved
* fixed error  in involved test
This commit is contained in:
Gerardo Roza
2019-05-26 15:30:12 -03:00
parent 20c2c2dd76
commit 33d0ac5738
9 changed files with 66 additions and 14 deletions

View File

@@ -0,0 +1,42 @@
package com.baeldung.model;
public class Book {
private int id;
private String author;
private String title;
public Book() {
}
public Book(int id, String author, String title) {
this.id = id;
this.author = author;
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}

View File

@@ -0,0 +1,31 @@
package com.baeldung.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baeldung.model.Book;
@Controller
@RequestMapping("books")
public class SimpleBookController {
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody Book getBook(@PathVariable int id) {
return findBookById(id);
}
private Book findBookById(int id) {
Book book = null;
if (id == 42) {
book = new Book();
book.setId(id);
book.setAuthor("Douglas Adamas");
book.setTitle("Hitchhiker's guide to the galaxy");
}
return book;
}
}

View File

@@ -0,0 +1,29 @@
package com.baeldung.web.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.model.Book;
@RestController
@RequestMapping("books-rest")
public class SimpleBookRestController {
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json")
public Book getBook(@PathVariable int id) {
return findBookById(id);
}
private Book findBookById(int id) {
Book book = null;
if (id == 42) {
book = new Book();
book.setId(id);
book.setAuthor("Douglas Adamas");
book.setTitle("Hitchhiker's guide to the galaxy");
}
return book;
}
}

View File

@@ -1,6 +1,6 @@
package com.baeldung;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

View File

@@ -0,0 +1,33 @@
package com.baeldung.web.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
public class SimpleBookControllerIntegrationTest {
private MockMvc mockMvc;
private static final String CONTENT_TYPE = "application/json;charset=UTF-8";
@BeforeEach
public void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(new SimpleBookController()).build();
}
@Test
public void givenBookId_whenMockMVC_thenVerifyResponse() throws Exception {
this.mockMvc
.perform(get("/books/42"))
.andExpect(status().isOk())
.andExpect(content().contentType(CONTENT_TYPE))
.andExpect(jsonPath("$.id").value(42));
}
}

View File

@@ -0,0 +1,33 @@
package com.baeldung.web.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
public class SimpleBookRestControllerIntegrationTest {
private MockMvc mockMvc;
private static final String CONTENT_TYPE = "application/json;charset=UTF-8";
@BeforeEach
public void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(new SimpleBookRestController()).build();
}
@Test
public void givenBookId_whenMockMVC_thenVerifyResponse() throws Exception {
this.mockMvc
.perform(get("/books-rest/42"))
.andExpect(status().isOk())
.andExpect(content().contentType(CONTENT_TYPE))
.andExpect(jsonPath("$.id").value(42));
}
}