Guest User

Spring Rest Test

a guest
Jan 16th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1.     @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json")
  2.     public ResponseEntity<Shop> createShop(@ModelAttribute Shop newShop) {
  3.         long newShopId = shopService.add(newShop);
  4.         Shop shop = shopService.get(newShopId);
  5.         return new ResponseEntity<Shop>(shop, HttpStatus.CREATED);
  6.     }
  7.  
  8.     @Test
  9.     public void testCreateShop() throws Exception {
  10.         Shop returnShop = Shop.builder().id(0L).name("shop1").latitude(1.0).latitude(1.0).build();
  11.         Shop shop = Shop.builder().id(1L).name("shop1").latitude(1.0).latitude(1.0).build();
  12.  
  13.         when(shopServiceMock.add(returnShop)).thenReturn(1L);
  14.         when(shopServiceMock.get(1L)).thenReturn(shop);
  15.  
  16.         ObjectMapper objectMapper = new ObjectMapper();
  17.         byte[] jsonShop = objectMapper.writeValueAsBytes(returnShop);
  18.  
  19.         mockMvc.perform(post("/api/shops/add").contentType(TestUtil.APPLICATION_JSON_UTF8).content(jsonShop))
  20.                 .andExpect(status().isCreated())
  21.                 .andExpect(jsonPath("$.id", is(1)))
  22.                 .andExpect(jsonPath("$.name", is("shop1")));
  23.  
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment