package com.pgf.mqspring.exception.handler; import static org.junit.jupiter.api.Assertions.*; import java.util.Objects; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import com.pgf.mqspring.component.MqSpringMessageSourceComponent; import com.pgf.mqspring.constant.MqSpringMessageId; import com.pgf.mqspring.exception.MqSpringException; import com.pgf.mqspring.model.ErrorResponseModel; class MqSpringExceptionHandlerTest01 { @InjectMocks MqSpringExceptionHandler mqSpringExceptionHandler; @Mock MqSpringMessageSourceComponent messageSource; @BeforeEach public void beforeEacn() { MockitoAnnotations.openMocks(this); } @Test void handleMqSpringException01() { Mockito.when(messageSource.getMessage(Mockito.any())).thenReturn("電文の組立に失敗しました。"); MqSpringException ex = assertThrows(MqSpringException.class, () -> test()); ResponseEntity entity = mqSpringExceptionHandler.handleMqSpringException(ex); assertEquals(true, check(entity)); } private void test() { throw new MqSpringException(MqSpringMessageId.SYS7070E, HttpStatus.BAD_REQUEST); } private boolean check(ResponseEntity entity) { if(!Objects.equals(entity.getBody().getResponseCode(), 400)) { return false; } if(!Objects.equals(entity.getBody().getSubErrorCode(), "SYS7070E")) { return false; } if(!Objects.equals(entity.getBody().getErrorMessage(), "電文の組立に失敗しました。")) { return false; } if(!Objects.equals(entity.getBody().getReportingComponentId(), "PGF_MQREST_API")) { return false; } return true; } }