package com.pgf.mqspring; import static org.junit.jupiter.api.Assertions.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import com.amazonaws.services.s3.AmazonS3; import com.pgf.mqspring.component.MqSpringMessageSourceComponent; import com.pgf.mqspring.component.TelegramInfoComponent; import com.pgf.mqspring.service.MqReceiveService; @SpringBootTest(properties = { "key.source.url=", "key.issuer=", "ibm.mq.queueName.send=", "ibm.mq.queueName.receive=" }) @AutoConfigureMockMvc class MqSpringApplicationTest03 { @Autowired private MockMvc mockMvc; @MockBean TelegramInfoComponent telegramInfoComponent; @InjectMocks MqSpringApplication mqSpringApplication; @Autowired WebApplicationContext webApplicationContext; @MockBean AmazonS3 s3Client; @Autowired ConfigurableApplicationContext context; @MockBean MqSpringMessageSourceComponent messageSource; @MockBean MqReceiveService service; @BeforeEach public void beforeEacn() { MockitoAnnotations.openMocks(this); mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test void recvMessage01() throws Exception { String id = "20220912093246881184"; ReflectionTestUtils.setField(mqSpringApplication, "context", context); mockMvc.perform( get("/recvMessage") .param("id", id)) .andExpect(status().is(200)); } @Test void recvMessage02() throws Exception { String id = ""; Logger logger = LogManager.getLogger(); ReflectionTestUtils.setField(mqSpringApplication, "context", context); ReflectionTestUtils.setField(mqSpringApplication, "logger", logger); Mockito.when(messageSource.getMessage(Mockito.any())).thenReturn("メッセージID取得エラー"); mockMvc.perform( get("/recvMessage") .param("id", id)) .andExpect(status().is(500)); assertTrue(true); } }