CustomerController { @PostMapping public ResponseEntity<Object> createCustomer(@Valid @RequestBody Customer customer, UriComponentsBuilder ucBuilder, Errors errors) { this.customerValidator.validate(customer, errors); if (errors.hasErrors()) { final String errorMessage = errors.getAllErrors().stream().map(ObjectError::getDefaultMessage) .collect(Collectors.joining(",")); final ApiError apiError = new ApiError(HttpStatus.UNPROCESSABLE_ENTITY, new Throwable(errorMessage)); log.error("Detailed Error while processing request :{}", apiError.toString()); return new ResponseEntity<>(apiError, apiError.getStatus()); } log.info("Creating Customer :{} ", customer.getFirstName()); if (this.customerService.isCustomerExist(customer.getFirstName())) { log.error("A Customer with name {} already exist ", customer.getFirstName()); return new ResponseEntity<>(HttpStatus.CONFLICT); } this.customerService.createCustomer(customer); final HttpHeaders headers = new HttpHeaders(); headers.setLocation(ucBuilder.path("/rest/customers/{customerId}").buildAndExpand(customer.getId()).toUri()); return new ResponseEntity<>(headers, HttpStatus.CREATED); }  @GetMapping ResponseEntity<List<Customer>> getCustomers(); @GetMapping(value = "{customerId}", produces = MediaType.APPLICATION_JSON_VALUE) ResponseEntity<Customer> getCustomer(@PathVariable("customerId") @NotBlank Long customerId); @PostMapping ResponseEntity<Object> createCustomer(@Valid @RequestBody Customer customer, UriComponentsBuilder ucBuilder,
			Errors errors); @PutMapping("{customerId}") ResponseEntity<Customer> updateCustomer(@RequestBody Customer updateCustomerRequest,
			@PathVariable("customerId") Long customerId); @DeleteMapping("{customerId}") ResponseEntity<Customer> removeCustomer(@PathVariable("customerId") Long customerId); @DeleteMapping ResponseEntity<Customer> deleteAllUsers();  }
@Test void testCreateCustomer() throws Exception { BDDMockito.willDoNothing().given(this.customerValidator).validate(ArgumentMatchers.any(), ArgumentMatchers.any(Errors.class)); given(this.customerService.isCustomerExist(ArgumentMatchers.anyString())).willReturn(false); given(this.customerService.createCustomer(ArgumentMatchers.any(Customer.class))).willReturn(null); this.mockMvc.perform(MockMvcRequestBuilders.post("/rest/customers/") .content(this.objectMapper.writeValueAsString(this.customer)).contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isCreated()); final Customer invalidCustomer = this.customer; invalidCustomer.setFirstName(null); this.mockMvc.perform(MockMvcRequestBuilders.post("/rest/customers/") .content(this.objectMapper.writeValueAsString(invalidCustomer)).contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isBadRequest()); }