CustomerControllerTest.java MY VERSION
unknown
java
2 years ago
3.8 kB
3
Indexable
package richard.springframework.spring6restmvc.controller; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; 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.MediaType; import org.springframework.test.web.servlet.MockMvc; import richard.springframework.spring6restmvc.model.Customer; import richard.springframework.spring6restmvc.services.CustomerService; import richard.springframework.spring6restmvc.services.CustomerServiceImpl; import java.time.LocalDateTime; import java.util.UUID; import static org.hamcrest.core.Is.is; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @WebMvcTest(CustomerController.class) class CustomerControllerTest { @MockBean CustomerService customerService; @Autowired MockMvc mockMvc; @Autowired ObjectMapper objectMapper; CustomerServiceImpl customerServiceImpl; @BeforeEach void setUp() { customerServiceImpl = new CustomerServiceImpl(); } @Test void testCreatCustomer() throws Exception { Customer customer = prepareCustomer(); customer.setId(null); customer.setVersion(null); given(customerService.saveNewCustomer(customer)) .willReturn(prepareStoredCustomer()); mockMvc.perform(post("/api/v1/customer").contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(customer))) .andExpect(status().isCreated()) .andExpect(header().exists("Location")); } private static Customer prepareStoredCustomer() { return Customer.builder() .id(UUID.randomUUID()) .name("Customer 2") .version(1) .createdDate(LocalDateTime.now()) .updateDate(LocalDateTime.now()) .build(); } private static Customer prepareCustomer() { return Customer.builder() .id(UUID.randomUUID()) .name("Customer 1") .version(1) .createdDate(LocalDateTime.now()) .updateDate(LocalDateTime.now()) .build(); } @Test void listAllCustomers() throws Exception { given(customerService.getAllCustomers()).willReturn(customerServiceImpl.getAllCustomers()); mockMvc.perform(get("/api/v1/customer") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.length()", is(3))); } @Test void getCustomerById() throws Exception { Customer customer = prepareCustomer(); given(customerService.getCustomerById(customer.getId())).willReturn(customer); mockMvc.perform(get("/api/v1/customer/" + customer.getId()) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.name", is(customer.getName()))); } }
Editor is loading...