Я запускаю тест с HttpURLConnection
. Но я хотел вернуть 204. В качестве кода ответа.
@Test
public void should_return_result_with_success_data() throws Exception {
HttpURLConnection urlConnection = PowerMockito.mock(HttpURLConnection.class);
URL finalUrl = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withArguments("http://sample.com").thenReturn(finalUrl);
PowerMockito.when(finalUrl.openConnection()).thenReturn(urlConnection);
PowerMockito.when(urlConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_NO_CONTENT);
task.call();
}
Осуществление
@Override
public EventResult call() throws Exception {
url = url.concat(URLEncoder.encode(data, StandardCharsets.UTF_8.name()));
HttpURLConnection connection = (HttpURLConnection) new URL("http://sample.com").openConnection();
connection.setConnectTimeout(connectionTimeout);
connection.setReadTimeout(readTimeout);
EventResult eventResult = new EventResult();
eventResult.setHttpStatusCode(connection.getResponseCode());
if (connection.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) {
return eventResult;
} else {
eventResult = JsonPojoConverter.getEventResult(IOUtils.toString(connection.getErrorStream(), StandardCharsets.UTF_8.name()));
}
return eventResult;
}
Почему он всегда возвращает код ответа 200. Есть ли обходной путь для возврата 204?