Skip to content

Commit

Permalink
adding new test
Browse files Browse the repository at this point in the history
  • Loading branch information
pram committed Mar 31, 2018
1 parent 87f2169 commit e2fdf11
Showing 1 changed file with 30 additions and 0 deletions.
@@ -1,5 +1,7 @@
package com.naughtyzombie.boilerplate.springreactboilerplate.resource;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.naughtyzombie.boilerplate.springreactboilerplate.model.Book;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -16,6 +18,7 @@
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand All @@ -27,6 +30,9 @@
})
public class BookResourceTest {

@Autowired
private ObjectMapper mapper;

@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
Expand All @@ -49,4 +55,28 @@ public void loadAllBooksRestTest() throws Exception {
JSONAssert.assertEquals(expected,result.getResponse().getContentAsString(), false);
}

@Test
public void addNewBooksRestTest() throws Exception {

Book book = new Book();
book.setId(2L);
book.setName("New Test Book");
book.setPrice(1.75);

String json = mapper.writeValueAsString(book);

MvcResult result = mockMvc.perform(post("/api/addbook")
.contentType(MediaType.APPLICATION_JSON)
.content(json)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andReturn();

String expected = "[{'id':1,'name':'Spring Boot React Example','price':0.0}," +
"{'id':2,'name':'New Test Book','price':1.75}]";

JSONAssert.assertEquals(expected,result.getResponse().getContentAsString(), false);
}

}

0 comments on commit e2fdf11

Please sign in to comment.