Вот соответствующий метод в моем классе ClientError4XxResponseHandler:
Код: Выделить всё
@Component
@Qualifier("clientError4XxResponseHandler")
public class ClientError4XxResponseHandler implements ErrorResponseHandler {
private static final Logger LOG LoggerFactory.getLogger(ClientError4XxResponseHandler .class);
private static final String BAD_REQUEST_CODE = "BAD_REQUEST";
private static final String DEFAULT_ERROR_MESSAGE = "Error while processing the request.";
@Override
public void handle(ClientHttpResponse response) throws IOException {
var responseString = IOUtils.toString(response.getBody());
try {
var mapper = new ObjectMapper();
var errorResponse = mapper.readValue(responseString, Response.class);
LOG.error("Business Error Response received with status code () and response {}", response.getStatusCode(), responseString);
throw ExceptionBuilder.buildBusinessException()
.setErrors(errorResponse.getErrors())
.setErrorCode(DEFAULT_CLIENT_ERROR_CODE)
.setHttpStatusCode(ResponseHttpStatus.valueOf(response.getStatusCode().value()))
.build();
}
catch (JsonProcessingException ex) {
LOG.error("Error while parsing the API error Response: {} {}", responseString, ex);
throw ExceptionBuilder.buildBusinessException()
.addMessage(Message.create(BAD_REQUEST_CODE, DEFAULT_ERROR_MESSAGE, responseString))
.setHttpStatusCode(ResponseHttpStatus.valueOf(response.getStatusCode().value()))
.setErrorCode(DEFAULT_CLIENT_ERROR_CODE)
.build();
}
finally {
response.close(); // This is the line that is removed in the mutant
}
}
}
Тестовый пример Спока:
Код: Выделить всё
def "test if response.close() is called"() {
given: "A mock response with necessary parameters"
def response = Mock(ClientHttpResponse)
def handler = new ClientError4XxResponseHandler()
// Mocking the behavior of the response
response.getStatusCode() >> HttpStatus.BAD_REQUEST
response.getBody() >> new ByteArrayInputStream('{"error": "Bad request"}'.getBytes())
when: "Handle the error response"
handler.handle(response)
then: "Verify that close() was called"
1 * response.close() // Verifying if close() was invoked exactly once
}
Я хочу убедиться, что был вызван метод response.close(), чтобы я мог убить мутанта, который удаляет вызов response.close().
Подробнее здесь: https://stackoverflow.com/questions/793 ... -in-spring