SHARE
TWEET

Untitled

wsx22 May 25th, 2019 173 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.rentalbike.controller;
  2.  
  3. import com.example.rentalbike.entity.Bike;
  4. import com.example.rentalbike.mapper.BikeMapper;
  5. import com.example.rentalbike.service.BikeService;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
  10. import org.springframework.boot.test.mock.mockito.MockBean;
  11. import org.springframework.test.context.junit4.SpringRunner;
  12. import org.springframework.test.web.servlet.MockMvc;
  13.  
  14. import java.util.List;
  15.  
  16. import static org.mockito.Mockito.verify;
  17. import static org.mockito.Mockito.when;
  18. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  19. import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
  20. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  21.  
  22. @RunWith(SpringRunner.class)
  23. @WebMvcTest(BikeController.class)
  24. public class BikeControllerTest {
  25.  
  26.     @Autowired
  27.     private MockMvc mockMvc;
  28.  
  29.     @MockBean
  30.     private BikeService bikeService;
  31.  
  32.     @MockBean
  33.     private BikeMapper bikeMapper;
  34.  
  35.     @Test
  36.     public void shouldReturnBikeDtoWhenTakenIsFalse() throws Exception {
  37.  
  38.         //given
  39.         List<Bike> bikes = prepareBikeDtoList();
  40.         when(bikeService.findAllByTakenIsFalse()).thenReturn(bikes);
  41.  
  42.         mockMvc.perform(get("/bikes/no-taken"))
  43.                 .andExpect(status().isOk())
  44.  
  45. //                .andExpect(content().json(
  46. //                        "[{\"id\":1,\"serialNumber\":\"AB123\",\"taken\":false}," +
  47. //                                    "{\"id\":2,\"serialNumber\":\"ZC553\",\"taken\":false}]"))
  48.                 .andDo(print());
  49.  
  50.         verify(bikeService).findAllByTakenIsFalse();
  51.     }
  52.  
  53.     private List<Bike> prepareBikeDtoList() {
  54.         return List.of(
  55.                 new Bike("AB123", false),
  56.                 new Bike("ZC553", false)
  57.         );
  58.     }
  59.  
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top