Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package coffee;
- import static org.junit.Assert.*;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
- import java.util.*;
- import org.hamcrest.Matchers;
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.mockito.Mockito;
- import org.skyscreamer.jsonassert.JSONAssert;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
- import org.springframework.boot.test.mock.mockito.MockBean;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.MediaType;
- import org.springframework.mock.web.MockHttpServletResponse;
- import org.springframework.test.context.junit4.SpringRunner;
- import org.springframework.test.web.servlet.MockMvc;
- import org.springframework.test.web.servlet.MvcResult;
- import org.springframework.test.web.servlet.RequestBuilder;
- import org.springframework.test.web.servlet.ResultActions;
- import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
- import coffee.controller.AboutController;
- import coffee.data.AboutRepository;
- @RunWith(SpringRunner.class)
- @WebMvcTest(value = AboutController.class, secure = false)
- public class AboutControllerTest {
- @Autowired
- private MockMvc mockMvc;
- @MockBean
- private AboutRepository aboutRepository;
- List<About> res;
- @Before
- public void setUp() {
- res = new ArrayList<About>();
- About about = new About();
- about.setName("Test");
- res.add(about);
- }
- @Test
- public void postAbouts() throws Exception{
- About about = res.get(0);
- Mockito.when(aboutRepository.save(about))
- .thenReturn(about);
- RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/abouts")
- .accept(MediaType.APPLICATION_JSON)
- .contentType(MediaType.APPLICATION_JSON)
- .content("[{'id':null,'position':null,'name':'Test','description':null,'image':null}]");
- MvcResult result = mockMvc.perform(requestBuilder)
- .andExpect(status().isOk())
- .andReturn();
- JSONAssert.assertEquals("[{'id':null,'position':null,'name':'Test','description':null,'image':null}]",
- result.getResponse().getContentAsString(),
- false);
- }
- }
- MockHttpServletRequest:
- HTTP Method = POST
- Request URI = /abouts
- Parameters = {}
- Headers = {Content-Type=[application/json], Accept=[application/json]}
- Body = <no character encoding set>
- Session Attrs = {}
- Handler:
- Type = coffee.controller.AboutController
- Method = public coffee.About coffee.controller.AboutController.postAbout(coffee.About)
- Async:
- Async started = false
- Async result = null
- Resolved Exception:
- Type = org.springframework.http.converter.HttpMessageNotReadableException
- ModelAndView:
- View name = null
- View = null
- Model = null
- FlashMap:
- Attributes = null
- MockHttpServletResponse:
- Status = 400
- Error message = null
- Headers = {}
- Content type = null
- Body =
- Forwarded URL = null
- Redirected URL = null
- Cookies = []
- 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
- package coffee.controller;
- import coffee.*;
- import coffee.data.AboutRepository;
- import java.util.Optional;
- import org.springframework.dao.EmptyResultDataAccessException;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.*;
- @RestController
- @RequestMapping(path="abouts",
- produces="application/json",
- consumes="application/json")
- @CrossOrigin(origins="*")
- public class AboutController {
- private AboutRepository aboutRepo;
- public AboutController(AboutRepository aboutRepo) {
- this.aboutRepo = aboutRepo;
- }
- @GetMapping
- public Iterable<About> getAbouts() {
- return aboutRepo.findAllByOrderByPosition();
- }
- @GetMapping("/{aboutId}")
- public ResponseEntity<About> aboutById(@PathVariable("aboutId") Long id) {
- Optional<About> optAbout = aboutRepo.findById(id);
- if (optAbout.isPresent()){
- return new ResponseEntity<>(optAbout.get(), HttpStatus.OK);
- }
- return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
- }
- @PostMapping
- @ResponseStatus(HttpStatus.CREATED)
- public About postAbout(@RequestBody About about) {
- return aboutRepo.save(about);
- }
- @PatchMapping("/{aboutId}")
- public About patchAbout(@PathVariable("aboutId") Long aboutId, @RequestBody About patch) {
- About about = aboutRepo.findById(aboutId).get();
- if (patch.getPosition() != null) {
- about.setPosition(patch.getPosition());
- }
- return aboutRepo.save(about);
- }
- @DeleteMapping("/{aboutId}")
- @ResponseStatus(code = HttpStatus.NO_CONTENT)
- public @ResponseBody void deleteEvent (@PathVariable("aboutId") Long aboutId) {
- try {
- aboutRepo.deleteById(aboutId);
- }catch (EmptyResultDataAccessException e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement