リクエスト パラメータが OK の場合はタスクを実行し、リクエスト パラメータが間違っているか空の場合は 401 Unauthorized HTTP ステータス コードを返す Web サービスを設計しました。
を使用RestTemplate
してテストを実行していますが、Web サービスが正常に応答した場合、HTTP 200 OK ステータスを確認できます。RestTemplate
ただし、それ自体が例外をスローするため、HTTP 401 エラーをテストできません。
私のテスト方法は
@Test
public void testUnauthorized()
{
Map<String, Object> params = new HashMap<String, Object>();
ResponseEntity response = restTemplate.postForEntity(url, params, Map.class);
Assert.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
Assert.assertNotNull(response.getBody());
}
例外ログは
org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:88)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:533)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:489)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:447)
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:318)
Web サービスが HTTP ステータス コード 401 で応答するかどうかをテストするにはどうすればよいですか?