CustomerServiceProxy implements CustomerService { @Override public void verifyCustomerCustomerId(String customerId) { Assert.notNull(customerServiceUrl, "Should not be null: " + customerServiceUrl); ResponseEntity<Customer> result = null; try { result = restTemplate.getForEntity(customerServiceUrl, Customer.class, customerId); } catch (HttpClientErrorException e) { switch (e.getStatusCode()) { case NOT_FOUND: throw new CustomerNotFoundException(); default: unrecognizedStatusCode(customerId, e.getStatusCode()); } } switch (result.getStatusCode()) { case OK: return; case NOT_FOUND: throw new CustomerNotFoundException(); default: unrecognizedStatusCode(customerId, result.getStatusCode()); } } @Inject  CustomerServiceProxy(RestTemplate restTemplate); void setCustomerServiceUrl(String customerServiceUrl); @Override void verifyCustomerCustomerId(String customerId);  }
@Test public void shouldFindCustomer() { when(restTemplate.getForEntity(CUSTOMER_SERVICE_URL, Customer.class, CUSTOMER_ID)) .thenReturn(new ResponseEntity<>(HttpStatus.OK)); proxy.verifyCustomerCustomerId(CUSTOMER_ID); verify(restTemplate).getForEntity(CUSTOMER_SERVICE_URL, Customer.class, CUSTOMER_ID); }
@Test(expected=CustomerNotFoundException.class) public void shouldNotFindCustomer() { when(restTemplate.getForEntity(CUSTOMER_SERVICE_URL, Customer.class, CUSTOMER_ID)) .thenReturn(new ResponseEntity<>(HttpStatus.NOT_FOUND)); proxy.verifyCustomerCustomerId(CUSTOMER_ID); }