1
1
package com .naughtyzombie .boilerplate .springreactboilerplate .resource ;
2
2
3
+ import com .fasterxml .jackson .databind .ObjectMapper ;
4
+ import com .naughtyzombie .boilerplate .springreactboilerplate .model .Book ;
3
5
import org .junit .Before ;
4
6
import org .junit .Test ;
5
7
import org .junit .runner .RunWith ;
16
18
import org .springframework .web .context .WebApplicationContext ;
17
19
18
20
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
21
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
19
22
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
20
23
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
21
24
27
30
})
28
31
public class BookResourceTest {
29
32
33
+ @ Autowired
34
+ private ObjectMapper mapper ;
35
+
30
36
@ Autowired
31
37
private WebApplicationContext wac ;
32
38
private MockMvc mockMvc ;
@@ -49,4 +55,28 @@ public void loadAllBooksRestTest() throws Exception {
49
55
JSONAssert .assertEquals (expected ,result .getResponse ().getContentAsString (), false );
50
56
}
51
57
58
+ @ Test
59
+ public void addNewBooksRestTest () throws Exception {
60
+
61
+ Book book = new Book ();
62
+ book .setId (2L );
63
+ book .setName ("New Test Book" );
64
+ book .setPrice (1.75 );
65
+
66
+ String json = mapper .writeValueAsString (book );
67
+
68
+ MvcResult result = mockMvc .perform (post ("/api/addbook" )
69
+ .contentType (MediaType .APPLICATION_JSON )
70
+ .content (json )
71
+ .accept (MediaType .APPLICATION_JSON ))
72
+ .andExpect (status ().isOk ())
73
+ .andExpect (content ().contentTypeCompatibleWith (MediaType .APPLICATION_JSON ))
74
+ .andReturn ();
75
+
76
+ String expected = "[{'id':1,'name':'Spring Boot React Example','price':0.0}," +
77
+ "{'id':2,'name':'New Test Book','price':1.75}]" ;
78
+
79
+ JSONAssert .assertEquals (expected ,result .getResponse ().getContentAsString (), false );
80
+ }
81
+
52
82
}
0 commit comments