### Question:
MarqetaClient { public CreateCardResponse createCard(CreateCardRequest request) { try { final String url = baseUrl + "/cards?show_pan=true&show_cvv_number=true"; return restTemplate.postForObject(url, request, CreateCardResponse.class); } catch (HttpStatusCodeException cause) { throw errorFactory.fromResponseBody(cause.getResponseBodyAsString(), cause.getStatusCode()); } } @Autowired  MarqetaClient(RestTemplate restTemplate, MarqetaErrorFactory errorFactory); void setBaseUrl(String baseUrl); CreateCardResponse createCard(CreateCardRequest request); CreateUserResponse createUser(CreateUserRequest request);  }### Answer:
@SuppressWarnings("ThrowableNotThrown") @Test(expected = MarqetaError.class) public void shouldThrowMarqetaErrorOnUnsuccessfulCreateCardInvocation() { final HttpStatusCodeException exception = new HttpClientErrorException(HttpStatus.BAD_REQUEST); doThrow(exception).when(restTemplate).postForObject(any(), any(), any(), any(Object[].class)); when(errorFactory.fromResponseBody(any(), any())).thenReturn(new MarqetaError()); try { target.createCard(new CreateCardRequest()); } finally { verify(errorFactory).fromResponseBody(exception.getResponseBodyAsString(), exception.getStatusCode()); } }
@Test public void shouldCallMarqetaApiOnSuccessfulCreateCardInvocation() { final CreateCardResponse response = new CreateCardResponse(); when(restTemplate.postForObject(any(), any(), any(), any(Object[].class))).thenReturn(response); final CreateCardRequest request = new CreateCardRequest(); assertThat(target.createCard(request), is(sameInstance(response))); verify(restTemplate).postForObject( eq("https: same(request), eq(CreateCardResponse.class), any(Object[].class) ); }