Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. package coffee;
  2.  
  3. import static org.junit.Assert.*;
  4. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
  5. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  6.  
  7. import java.util.*;
  8.  
  9. import org.hamcrest.Matchers;
  10. import org.junit.Before;
  11. import org.junit.Test;
  12. import org.junit.runner.RunWith;
  13. import org.mockito.Mockito;
  14. import org.skyscreamer.jsonassert.JSONAssert;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
  17. import org.springframework.boot.test.mock.mockito.MockBean;
  18. import org.springframework.http.HttpStatus;
  19. import org.springframework.http.MediaType;
  20. import org.springframework.mock.web.MockHttpServletResponse;
  21. import org.springframework.test.context.junit4.SpringRunner;
  22. import org.springframework.test.web.servlet.MockMvc;
  23. import org.springframework.test.web.servlet.MvcResult;
  24. import org.springframework.test.web.servlet.RequestBuilder;
  25. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
  26.  
  27. import coffee.controller.AboutController;
  28. import coffee.data.AboutRepository;
  29.  
  30. @RunWith(SpringRunner.class)
  31. @WebMvcTest(value = AboutController.class, secure = false)
  32. public class AboutControllerTest {
  33.  
  34. @Autowired
  35. private MockMvc mockMvc;
  36.  
  37. @MockBean
  38. private AboutRepository aboutRepository;
  39.  
  40. List<About> res;
  41.  
  42. @Before
  43. public void setUp() {
  44. res = new ArrayList<About>();
  45. About about = new About();
  46. about.setName("Test");
  47. res.add(about);
  48. }
  49.  
  50. @Test
  51. public void getAbouts() throws Exception{
  52.  
  53. Mockito.when(aboutRepository.findAllByOrderByPosition())
  54. .thenReturn(res);
  55.  
  56. RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/abouts")
  57. .accept(MediaType.APPLICATION_JSON);
  58.  
  59. mockMvc.perform(requestBuilder)
  60. .andExpect(status().isOk())
  61. .andExpect(jsonPath("$.name", Matchers.is("Test")));
  62.  
  63. }
  64.  
  65. @Test
  66. public void postAbouts() throws Exception{
  67. About about = res.get(0);
  68.  
  69. Mockito.when(aboutRepository.save(about))
  70. .thenReturn(about);
  71.  
  72. RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/abouts")
  73. .accept(MediaType.APPLICATION_JSON);
  74.  
  75. mockMvc.perform(requestBuilder)
  76. .andExpect(status().isOk())
  77. .andExpect(jsonPath("$.name", Matchers.is("Test")));
  78. }
  79. }
  80.  
  81. MockHttpServletRequest:
  82. HTTP Method = GET
  83. Request URI = /abouts
  84. Parameters = {}
  85. Headers = {Accept=[application/json]}
  86. Body = <no character encoding set>
  87. Session Attrs = {}
  88.  
  89. Handler:
  90. Type = null
  91.  
  92. Async:
  93. Async started = false
  94. Async result = null
  95.  
  96. Resolved Exception:
  97. Type = org.springframework.web.HttpMediaTypeNotSupportedException
  98.  
  99. ModelAndView:
  100. View name = null
  101. View = null
  102. Model = null
  103.  
  104. FlashMap:
  105. Attributes = null
  106.  
  107. MockHttpServletResponse:
  108. Status = 415
  109. Error message = null
  110. Headers = {Accept=[application/json]}
  111. Content type = null
  112. Body =
  113. Forwarded URL = null
  114. Redirected URL = null
  115. Cookies = []
  116. 2019-05-21 14:07:35.345 WARN 1911 --- [ main] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported]
  117.  
  118. MockHttpServletRequest:
  119. HTTP Method = GET
  120. Request URI = /abouts
  121. Parameters = {}
  122. Headers = {Accept=[application/json]}
  123. Body = <no character encoding set>
  124. Session Attrs = {}
  125.  
  126. Handler:
  127. Type = null
  128.  
  129. Async:
  130. Async started = false
  131. Async result = null
  132.  
  133. Resolved Exception:
  134. Type = org.springframework.web.HttpMediaTypeNotSupportedException
  135.  
  136. ModelAndView:
  137. View name = null
  138. View = null
  139. Model = null
  140.  
  141. FlashMap:
  142. Attributes = null
  143.  
  144. MockHttpServletResponse:
  145. Status = 415
  146. Error message = null
  147. Headers = {Accept=[application/json]}
  148. Content type = null
  149. Body =
  150. Forwarded URL = null
  151. Redirected URL = null
  152. Cookies = []
  153.  
  154. MockHttpServletRequest:
  155. HTTP Method = POST
  156. Request URI = /abouts
  157. Parameters = {}
  158. Headers = {Accept=[application/json]}
  159. Body = <no character encoding set>
  160. Session Attrs = {}
  161.  
  162. Handler:
  163. Type = null
  164.  
  165. Async:
  166. Async started = false
  167. Async result = null
  168.  
  169. Resolved Exception:
  170. Type = org.springframework.web.HttpMediaTypeNotSupportedException
  171.  
  172. ModelAndView:
  173. View name = null
  174. View = null
  175. Model = null
  176.  
  177. FlashMap:
  178. Attributes = null
  179.  
  180. MockHttpServletResponse:
  181. Status = 415
  182. Error message = null
  183. Headers = {Accept=[application/json]}
  184. Content type = null
  185. Body =
  186. Forwarded URL = null
  187. Redirected URL = null
  188. Cookies = []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement