カスタムアクチュエータエンドポイントをテストする方法
17616 ワード
以前は、「JIRAを監視するためにカスタムエンドポイントを作成する方法」について投稿しましたが、これは2番目の部分です.
以下に必要な依存関係の一覧を示します.
そうするために、我々はunirestをモックする必要があります.get ()コールとレスポンス.
我々は1つだけ@ runwith注釈を使用することができますので、私たちは、春のコンテキストを読み込むには、PowerMokRunnerDelegate(Springrunner . class)を追加する理由です.
次に、エンドポイントをテストします.
以下に必要な依存関係の一覧を示します.
implementation 'junit:junit:4.12'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
exclude group: "com.vaadin.external.google", module:"android-json"
}
testImplementation group: 'com.konghq', name: 'unirest-mocks', version: '3.11.06'
testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.9'
testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.9'
JiraconnectorServiceで使用される設定クラスを読み込むクラスが必要です.@TestConfiguration
public class TestConfig {
@Bean
public JiraConfig getJiraConfig(){
return new JiraConfig();
}
}
JIRAエンドポイント応答に従って良いデータを返すようにサービスをテストします.そうするために、我々はunirestをモックする必要があります.get ()コールとレスポンス.
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest(Unirest.class)
@Import({TestConfig.class})
@PowerMockIgnore({"javax.*.*", "com.sun.*", "org.xml.*"})
@SpringBootTest
public class JiraConnectorServiceTest {
@Mock
private GetRequest getRequest;
@Autowired
JiraConnectorService jiraConnectorService;
@Autowired
JiraConfig jiraConfig;
@Test
public void getResponseTimeTest() throws Exception {
JsonNode json = new JsonNode("{\"result\":10}");
HttpResponse<JsonNode> mockResponse = mock(HttpResponse.class);
when(mockResponse.getStatus()).thenReturn(200);
when(mockResponse.getBody()).thenReturn(json);
String mySelfEndPointUrl = jiraConfig.getHost() + jiraConfig.getApiPath() + JiraConnectorService.JIRA_MYSELF_ENDPOINT;
PowerMockito.mockStatic(Unirest.class);
when(Unirest.get(mySelfEndPointUrl)).thenReturn(getRequest);
when(getRequest.header(JiraConnectorService.HEADER_ACCEPT, JiraConnectorService.HEADER_APP_JSON)).thenReturn(getRequest);
when(getRequest.basicAuth(jiraConfig.getUser(), jiraConfig.getPassword())).thenReturn(getRequest);
when(getRequest.asJson()).thenReturn(mockResponse);
ResponseTimeData data = jiraConnectorService.getResponseTime();
Assert.assertEquals(HttpStatus.OK.value(), data.getHttpStatusCode());
Assert.assertTrue(data.getTime() > 0);
}
静的メソッドUnirestをモックするにはPowerMocKitと@ RunWas(Powermockrunner . Class)注釈を使用する必要があります.…を得る我々は1つだけ@ runwith注釈を使用することができますので、私たちは、春のコンテキストを読み込むには、PowerMokRunnerDelegate(Springrunner . class)を追加する理由です.
次に、エンドポイントをテストします.
@RunWith(SpringRunner.class)
@Import({TestConfig.class})
@AutoConfigureMockMvc
@SpringBootTest
public class RestJiraEndPointTest {
private static final String ACTUATOR_URI = "/management";
@MockBean
private JiraConnectorService jiraConnectorService;
@Autowired
private MockMvc mockMvc;
@Test
public void healthDtl_DOWN() throws Exception {
ResponseTimeData data = new ResponseTimeData();
data.setTime(-1);
data.setHttpStatusCode(HttpStatus.SERVICE_UNAVAILABLE.value());
data.setMessage("Service unavailable");
Mockito.when(jiraConnectorService.getResponseTime()).thenReturn(data);
RequestBuilder requestBuilder = MockMvcRequestBuilders.get(ACTUATOR_URI + "/jira/healthDtl")
.accept(MediaType.APPLICATION_JSON);
this.mockMvc.perform(requestBuilder)
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.status").value("DOWN"));
}
@Test
public void healthDtl_UP() throws Exception {
ResponseTimeData data = new ResponseTimeData();
data.setTime(235L);
data.setHttpStatusCode(HttpStatus.OK.value());
data.setMessage("Ok");
Mockito.when(jiraConnectorService.getResponseTime()).thenReturn(data);
RequestBuilder requestBuilder = MockMvcRequestBuilders.get(ACTUATOR_URI + "/jira/healthDtl")
.accept(MediaType.APPLICATION_JSON);
this.mockMvc.perform(requestBuilder)
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.status").value("UP"))
.andExpect(MockMvcResultMatchers.jsonPath("$.responseTimeMs").value("235"));
}
}
私は最初に@ springboottestの代わりに@ webmvctestを使ってみました.スプリングはRESTコントローラとして@ restcontroller erpointを認識していないようです.したがって、@ springboottestと@ autoconfiguremockmvcを使わなければなりません.Reference
この問題について(カスタムアクチュエータエンドポイントをテストする方法), 我々は、より多くの情報をここで見つけました https://dev.to/optnc/how-to-unit-test-a-custom-actuator-endpoint-3h4pテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol