### Question:
RecaptchaClientService { public boolean verify(String ipAddress, String recaptchaResponse) throws RecaptchaException { if (!recaptchaConfigHolder.isEnabled()) { return true; } if (recaptchaResponse == null) { return false; } RequestEntity requestEntity = RequestEntity .post(getSiteVerifyURI(ipAddress, recaptchaResponse)) .accept(MediaType.APPLICATION_JSON) .build(); ResponseEntity<RecaptchaResponse> responseEntity = restTemplate.exchange(requestEntity, RecaptchaResponse.class); if (responseEntity.getBody() != null) { if (responseEntity.getBody().isSuccess()) { return true; } else { List<String> errorCodes = Arrays.asList(responseEntity.getBody().getErrorCodes()); if (errorCodes.contains(MISSING_INPUT_RESPONSE) || errorCodes.contains(INVALID_INPUT_RESPONSE)) { return false; } else { throw getException(errorCodes); } } } throw new RecaptchaException("Error in the recaptcha verify client."); } @Autowired  RecaptchaClientService(@Qualifier("restTemplateRecaptcha") RestTemplate restTemplate,
                                  RecaptchaConfigHolder recaptchaConfigHolder); boolean verify(String ipAddress, String recaptchaResponse); static final String MISSING_INPUT_SECRET; static final String INVALID_INPUT_SECRET; static final String MISSING_INPUT_RESPONSE; static final String INVALID_INPUT_RESPONSE; static final String RECAPTCHA_BASE_URL; }### Answer:
@Test public void testVerify() { RecaptchaResponse recaptchaResponse = new RecaptchaResponse(true, new Date(), "hostname"); ResponseEntity responseEntity = new ResponseEntity(recaptchaResponse, HttpStatus.OK); when(restTemplate.exchange(any(), eq(RecaptchaResponse.class))).thenReturn(responseEntity); boolean result = recaptchaClientService.verify("1.2.3.4", "recaptcha-response-test"); assertTrue(result); }
@Test public void testVerify_Not_Enabled() { doReturn(false).when(recaptchaConfigHolder).isEnabled(); boolean result = recaptchaClientService.verify("1.2.3.4", "recaptcha-response-test"); assertTrue(result); }
@Test public void testVerify_Null_Response() { boolean result = recaptchaClientService.verify("1.2.3.4", null); assertFalse(result); }
@Test(expected = RecaptchaException.class) public void testVerify_Internal_Server_Error() { ResponseEntity responseEntity = new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR); when(restTemplate.exchange(any(), eq(RecaptchaResponse.class))).thenReturn(responseEntity); boolean result = recaptchaClientService.verify("1.2.3.4", "recaptcha-response-test"); assertFalse(result); }
@Test public void testVerify_Invalid_Input_Response() { String[] errors = new String[]{RecaptchaClientService.INVALID_INPUT_RESPONSE}; RecaptchaResponse recaptchaResponse = new RecaptchaResponse(false, new Date(), "hostname", errors); ResponseEntity responseEntity = new ResponseEntity(recaptchaResponse, HttpStatus.OK); when(restTemplate.exchange(any(), eq(RecaptchaResponse.class))).thenReturn(responseEntity); boolean result = recaptchaClientService.verify("1.2.3.4", "recaptcha-response-test"); assertFalse(result); }
@Test public void testVerify_Missing_Input_Response() { String[] errors = new String[]{RecaptchaClientService.MISSING_INPUT_RESPONSE}; RecaptchaResponse recaptchaResponse = new RecaptchaResponse(false, new Date(), "hostname", errors); ResponseEntity responseEntity = new ResponseEntity(recaptchaResponse, HttpStatus.OK); when(restTemplate.exchange(any(), eq(RecaptchaResponse.class))).thenReturn(responseEntity); boolean result = recaptchaClientService.verify("1.2.3.4", "recaptcha-response-test"); assertFalse(result); }