Skip to content

Commit e2fdf11

Browse files
committed
adding new test
1 parent 87f2169 commit e2fdf11

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/test/java/com/naughtyzombie/boilerplate/springreactboilerplate/resource/BookResourceTest.java

+30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.naughtyzombie.boilerplate.springreactboilerplate.resource;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.naughtyzombie.boilerplate.springreactboilerplate.model.Book;
35
import org.junit.Before;
46
import org.junit.Test;
57
import org.junit.runner.RunWith;
@@ -16,6 +18,7 @@
1618
import org.springframework.web.context.WebApplicationContext;
1719

1820
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
21+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
1922
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
2023
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2124

@@ -27,6 +30,9 @@
2730
})
2831
public class BookResourceTest {
2932

33+
@Autowired
34+
private ObjectMapper mapper;
35+
3036
@Autowired
3137
private WebApplicationContext wac;
3238
private MockMvc mockMvc;
@@ -49,4 +55,28 @@ public void loadAllBooksRestTest() throws Exception {
4955
JSONAssert.assertEquals(expected,result.getResponse().getContentAsString(), false);
5056
}
5157

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+
5282
}

0 commit comments

Comments
 (0)