|
1 | 1 | package com.answerdigital.answerking; |
2 | 2 |
|
3 | 3 | import com.answerdigital.answerking.utility.AbstractContainerBaseTest; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
4 | 5 | import org.junit.jupiter.api.Test; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
5 | 8 | import org.springframework.boot.test.context.SpringBootTest; |
| 9 | +import org.springframework.http.HttpStatus; |
| 10 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 11 | +import org.springframework.test.web.servlet.MockMvc; |
| 12 | +import org.springframework.test.web.servlet.RequestBuilder; |
| 13 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
6 | 17 |
|
7 | 18 | @SpringBootTest |
| 19 | +@AutoConfigureMockMvc(addFilters = false) |
8 | 20 | class AnswerKingApplicationTest extends AbstractContainerBaseTest |
9 | 21 | { |
| 22 | + @Autowired |
| 23 | + private MockMvc mockMvc; |
| 24 | + |
10 | 25 | @Test |
11 | | - void contextLoads(){ |
| 26 | + void getAllProductsReturnListOfProductObjects() throws Exception { |
| 27 | + //when |
| 28 | + RequestBuilder request = MockMvcRequestBuilders.get("/products"); |
| 29 | + MockHttpServletResponse response = mockMvc.perform(request).andReturn().getResponse(); |
12 | 30 |
|
| 31 | + //then |
| 32 | + assertEquals(HttpStatus.OK.value(), response.getStatus()); |
| 33 | + assertFalse(response.getContentAsString().isEmpty()); |
| 34 | + ObjectMapper mapper = new ObjectMapper(); |
| 35 | + assertEquals("Burger", mapper.readTree(response.getContentAsString()).get(0).get("name").textValue()); |
| 36 | + assertEquals("300g Beef", mapper.readTree(response.getContentAsString()).get(0).get("description").textValue()); |
| 37 | + assertEquals(6.69, mapper.readTree(response.getContentAsString()).get(0).get("price").asDouble()); |
13 | 38 | } |
14 | 39 | } |
0 commit comments