Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 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.ResultActions;
  26. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
  27.  
  28. import coffee.controller.AboutController;
  29. import coffee.data.AboutRepository;
  30.  
  31. @RunWith(SpringRunner.class)
  32. @WebMvcTest(value = AboutController.class, secure = false)
  33. public class AboutControllerTest {
  34.  
  35. @Autowired
  36. private MockMvc mockMvc;
  37.  
  38. @MockBean
  39. private AboutRepository aboutRepository;
  40.  
  41. List<About> res;
  42.  
  43. @Before
  44. public void setUp() {
  45. res = new ArrayList<About>();
  46. About about = new About();
  47. about.setName("Test");
  48. res.add(about);
  49. }
  50.  
  51. @Test
  52. public void postAbouts() throws Exception{
  53. About about = res.get(0);
  54.  
  55. Mockito.when(aboutRepository.save(about))
  56. .thenReturn(about);
  57.  
  58. RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/abouts")
  59. .accept(MediaType.APPLICATION_JSON)
  60. .contentType(MediaType.APPLICATION_JSON)
  61. .content("[{'id':null,'position':null,'name':'Test','description':null,'image':null}]");
  62.  
  63. MvcResult result = mockMvc.perform(requestBuilder)
  64. .andExpect(status().isOk())
  65. .andReturn();
  66.  
  67. JSONAssert.assertEquals("[{'id':null,'position':null,'name':'Test','description':null,'image':null}]",
  68. result.getResponse().getContentAsString(),
  69. false);
  70. }
  71. }
  72.  
  73. MockHttpServletRequest:
  74. HTTP Method = POST
  75. Request URI = /abouts
  76. Parameters = {}
  77. Headers = {Content-Type=[application/json], Accept=[application/json]}
  78. Body = <no character encoding set>
  79. Session Attrs = {}
  80.  
  81. Handler:
  82. Type = coffee.controller.AboutController
  83. Method = public coffee.About coffee.controller.AboutController.postAbout(coffee.About)
  84.  
  85. Async:
  86. Async started = false
  87. Async result = null
  88.  
  89. Resolved Exception:
  90. Type = org.springframework.http.converter.HttpMessageNotReadableException
  91.  
  92. ModelAndView:
  93. View name = null
  94. View = null
  95. Model = null
  96.  
  97. FlashMap:
  98. Attributes = null
  99.  
  100. MockHttpServletResponse:
  101. Status = 400
  102. Error message = null
  103. Headers = {}
  104. Content type = null
  105. Body =
  106. Forwarded URL = null
  107. Redirected URL = null
  108. Cookies = []
  109. 2019-05-21 14:47:56.035 INFO 1977 --- [ Thread-4] o.s.w.c.s.GenericWebApplicationContext : Closing org.springframework.web.context.support.GenericWebApplicationContext@7fd50002: startup date [Tue May 21 14:47:54 PDT 2019]; root of context hierarchy
  110.  
  111. package coffee.controller;
  112.  
  113. import coffee.*;
  114. import coffee.data.AboutRepository;
  115.  
  116. import java.util.Optional;
  117.  
  118. import org.springframework.dao.EmptyResultDataAccessException;
  119. import org.springframework.http.HttpStatus;
  120. import org.springframework.http.ResponseEntity;
  121. import org.springframework.web.bind.annotation.*;
  122.  
  123.  
  124.  
  125. @RestController
  126. @RequestMapping(path="abouts",
  127. produces="application/json",
  128. consumes="application/json")
  129. @CrossOrigin(origins="*")
  130. public class AboutController {
  131.  
  132. private AboutRepository aboutRepo;
  133.  
  134. public AboutController(AboutRepository aboutRepo) {
  135. this.aboutRepo = aboutRepo;
  136. }
  137.  
  138. @GetMapping
  139. public Iterable<About> getAbouts() {
  140. return aboutRepo.findAllByOrderByPosition();
  141. }
  142.  
  143. @GetMapping("/{aboutId}")
  144. public ResponseEntity<About> aboutById(@PathVariable("aboutId") Long id) {
  145. Optional<About> optAbout = aboutRepo.findById(id);
  146. if (optAbout.isPresent()){
  147. return new ResponseEntity<>(optAbout.get(), HttpStatus.OK);
  148. }
  149. return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
  150. }
  151.  
  152. @PostMapping
  153. @ResponseStatus(HttpStatus.CREATED)
  154. public About postAbout(@RequestBody About about) {
  155. return aboutRepo.save(about);
  156. }
  157.  
  158. @PatchMapping("/{aboutId}")
  159. public About patchAbout(@PathVariable("aboutId") Long aboutId, @RequestBody About patch) {
  160.  
  161. About about = aboutRepo.findById(aboutId).get();
  162. if (patch.getPosition() != null) {
  163. about.setPosition(patch.getPosition());
  164. }
  165. return aboutRepo.save(about);
  166. }
  167.  
  168. @DeleteMapping("/{aboutId}")
  169. @ResponseStatus(code = HttpStatus.NO_CONTENT)
  170. public @ResponseBody void deleteEvent (@PathVariable("aboutId") Long aboutId) {
  171. try {
  172. aboutRepo.deleteById(aboutId);
  173. }catch (EmptyResultDataAccessException e) {
  174. }
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement