File tree Expand file tree Collapse file tree
test/java/com/baeldung/web/controller Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .model ;
2+
3+ public class Book {
4+
5+ private int id ;
6+ private String author ;
7+ private String title ;
8+
9+ public int getId () {
10+ return id ;
11+ }
12+
13+ public void setId (int id ) {
14+ this .id = id ;
15+ }
16+
17+ public String getAuthor () {
18+ return author ;
19+ }
20+
21+ public void setAuthor (String author ) {
22+ this .author = author ;
23+ }
24+
25+ public String getTitle () {
26+ return title ;
27+ }
28+
29+ public void setTitle (String title ) {
30+ this .title = title ;
31+ }
32+
33+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .web .controller ;
2+
3+ import org .springframework .stereotype .Controller ;
4+ import org .springframework .web .bind .annotation .PathVariable ;
5+ import org .springframework .web .bind .annotation .RequestMapping ;
6+ import org .springframework .web .bind .annotation .RequestMethod ;
7+ import org .springframework .web .bind .annotation .ResponseBody ;
8+
9+ import com .baeldung .model .Book ;
10+
11+ @ Controller
12+ @ RequestMapping ("books" )
13+ public class SimpleBookController {
14+
15+ @ RequestMapping (value = "/{id}" , method = RequestMethod .GET , produces = "application/json" )
16+ public @ ResponseBody Book getBook (@ PathVariable int id ) {
17+ return findBookById (id );
18+ }
19+
20+ private Book findBookById (int id ) {
21+ Book book = null ;
22+ if (id == 42 ) {
23+ book = new Book ();
24+ book .setId (id );
25+ book .setAuthor ("Douglas Adamas" );
26+ book .setTitle ("Hitchhiker's guide to the galaxy" );
27+ }
28+ return book ;
29+ }
30+
31+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .web .controller ;
2+
3+ import org .springframework .web .bind .annotation .PathVariable ;
4+ import org .springframework .web .bind .annotation .RequestMapping ;
5+ import org .springframework .web .bind .annotation .RequestMethod ;
6+ import org .springframework .web .bind .annotation .RestController ;
7+
8+ import com .baeldung .model .Book ;
9+
10+ @ RestController
11+ @ RequestMapping ("books-rest" )
12+ public class SimpleBookRestController {
13+
14+ @ RequestMapping (value = "/{id}" , method = RequestMethod .GET , produces = "application/json" )
15+ public Book getBook (@ PathVariable int id ) {
16+ return findBookById (id );
17+ }
18+
19+ private Book findBookById (int id ) {
20+ Book book = null ;
21+ if (id == 42 ) {
22+ book = new Book ();
23+ book .setId (id );
24+ book .setAuthor ("Douglas Adamas" );
25+ book .setTitle ("Hitchhiker's guide to the galaxy" );
26+ }
27+ return book ;
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .web .controller ;
2+
3+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
4+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
5+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
6+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
7+
8+ import org .junit .Before ;
9+ import org .junit .Test ;
10+ import org .springframework .test .web .servlet .MockMvc ;
11+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
12+
13+ import com .baeldung .web .controller .SimpleBookController ;
14+
15+ public class SimpleBookControllerTest {
16+
17+ private MockMvc mockMvc ;
18+ private static final String CONTENT_TYPE = "application/json;charset=UTF-8" ;
19+
20+ @ Before
21+ public void setup () {
22+ this .mockMvc = MockMvcBuilders .standaloneSetup (new SimpleBookController ()).build ();
23+ }
24+
25+ @ Test
26+ public void givenBookId_whenMockMVC_thenVerifyResponse () throws Exception {
27+ this .mockMvc
28+ .perform (get ("/books/42" ))
29+ .andExpect (status ().isOk ())
30+ .andExpect (content ().contentType (CONTENT_TYPE ))
31+ .andExpect (jsonPath ("$.id" ).value (42 ));
32+ }
33+
34+ }
35+
Original file line number Diff line number Diff line change 1+ package com .baeldung .web .controller ;
2+
3+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
4+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
5+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
6+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
7+
8+ import org .junit .Before ;
9+ import org .junit .Test ;
10+ import org .springframework .test .web .servlet .MockMvc ;
11+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
12+
13+ import com .baeldung .web .controller .SimpleBookController ;
14+
15+ public class SimpleBookRestControllerTest {
16+
17+ private MockMvc mockMvc ;
18+ private static final String CONTENT_TYPE = "application/json;charset=UTF-8" ;
19+
20+ @ Before
21+ public void setup () {
22+ this .mockMvc = MockMvcBuilders .standaloneSetup (new SimpleBookController ()).build ();
23+ }
24+
25+ @ Test
26+ public void givenBookId_whenMockMVC_thenVerifyResponse () throws Exception {
27+ this .mockMvc
28+ .perform (get ("/books/42" ))
29+ .andExpect (status ().isOk ())
30+ .andExpect (content ().contentType (CONTENT_TYPE ))
31+ .andExpect (jsonPath ("$.id" ).value (42 ));
32+ }
33+
34+ }
35+
You can’t perform that action at this time.
0 commit comments