### Question:
TppErrorMessageWriter { public void writeError(HttpServletResponse response, TppErrorMessage tppErrorMessage) throws IOException { MessageErrorCode messageErrorCode = tppErrorMessage.getCode(); MessageError messageError = new MessageError(getErrorType(messageErrorCode.getCode()), TppMessageInformation.of(tppErrorMessage.getCategory(), messageErrorCode, tppErrorMessage.getTextParams())); writeMessageError(response, messageError); }  void writeError(HttpServletResponse response, TppErrorMessage tppErrorMessage); void writeError(HttpServletResponse response, MessageError messageError); void writeServiceUnavailableError(HttpServletResponse response, String message);  }### Answer:
@Test void writeError_wrongServiceType() { TppErrorMessage tppErrorMessage = new TppErrorMessage(MESSAGE_CATEGORY, MESSAGE_ERROR_CODE, MESSAGE_ERROR_STRING); assertThrows(IllegalArgumentException.class, () -> tppErrorMessageWriter.writeError(response, tppErrorMessage)); verify(serviceTypeDiscoveryService).getServiceType(); }
@Test void writeError_successful() throws IOException { when(serviceTypeDiscoveryService.getServiceType()).thenReturn(SERVICE_TYPE); TppErrorMessage tppErrorMessage = new TppErrorMessage(MESSAGE_CATEGORY, MESSAGE_ERROR_CODE, MESSAGE_ERROR_STRING); MessageError messageError = new MessageError(ErrorType.AIS_400, TppMessageInformation.of(tppErrorMessage.getCategory(), tppErrorMessage.getCode(), tppErrorMessage.getTextParams())); when(errorMapperContainer.getErrorBody(messageError)).thenReturn(ERROR_BODY); when(response.getWriter()).thenReturn(PRINT_WRITER); tppErrorMessageWriter.writeError(response, tppErrorMessage); ArgumentCaptor<Writer> writerArgumentCaptor = ArgumentCaptor.forClass(Writer.class); ArgumentCaptor<ErrorMapperContainer.ErrorBody> errorBodyArgumentCaptor = ArgumentCaptor.forClass(ErrorMapperContainer.ErrorBody.class); verify(serviceTypeDiscoveryService).getServiceType(); verify(errorMapperContainer).getErrorBody(messageError); verify(xs2aObjectMapper).writeValue(writerArgumentCaptor.capture(), errorBodyArgumentCaptor.capture()); assertEquals(PRINT_WRITER, writerArgumentCaptor.getValue()); assertEquals(ERROR_BODY.getBody(), errorBodyArgumentCaptor.getValue()); }
@Test void writeMessageError_successful() throws IOException { MessageError messageError = new MessageError(ErrorType.AIS_400, TppMessageInformation.buildWithCustomError(MESSAGE_ERROR_CODE, MESSAGE_ERROR_STRING)); ErrorMapperContainer.ErrorBody errorBody = new ErrorMapperContainer.ErrorBody(messageError, HttpStatus.OK); when(errorMapperContainer.getErrorBody(messageError)).thenReturn(errorBody); when(response.getWriter()).thenReturn(PRINT_WRITER); tppErrorMessageWriter.writeError(response, messageError); ArgumentCaptor<Writer> writerArgumentCaptor = ArgumentCaptor.forClass(Writer.class); ArgumentCaptor<ErrorMapperContainer.ErrorBody> errorBodyArgumentCaptor = ArgumentCaptor.forClass(ErrorMapperContainer.ErrorBody.class); verify(errorMapperContainer).getErrorBody(messageError); verify(xs2aObjectMapper).writeValue(writerArgumentCaptor.capture(), errorBodyArgumentCaptor.capture()); assertEquals(PRINT_WRITER, writerArgumentCaptor.getValue()); assertEquals(errorBody.getBody(), errorBodyArgumentCaptor.getValue()); }