1616import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .options ;
1717import static org .assertj .core .api .Assertions .assertThat ;
1818import static org .assertj .core .api .Assertions .failBecauseExceptionWasNotThrown ;
19+ import static org .junit .jupiter .api .Assertions .assertThrows ;
1920
2021import com .github .tomakehurst .wiremock .junit5 .WireMockExtension ;
2122import io .kubernetes .client .openapi .ApiClient ;
@@ -49,31 +50,21 @@ void getPodMetricsThrowsAPIExceptionWhenServerReturnsError() {
4950 .withStatus (503 )
5051 .withHeader ("Content-Type" , "text/plain" )
5152 .withBody ("Service Unavailable" )));
52- try {
53- metrics .getPodMetrics (namespace );
54- failBecauseExceptionWasNotThrown (ApiException .class );
55- } catch (ApiException ex ) {
56- assertThat (ex .getCode ()).isEqualTo (503 );
57- }
53+ assertThrows (ApiException .class , () -> metrics .getPodMetrics (namespace ));
5854 }
5955
6056 @ Test
6157 void getPodMetricsWithLabelSelectorThrowsAPIExceptionWhenServerReturnsError () {
6258 String namespace = "default" ;
6359 Metrics metrics = new Metrics (client );
6460 apiServer .stubFor (
65- get (urlPathMatching ("^/apis/metrics.k8s.io/v1beta1/namespaces/" + namespace + "/pods.*" ))
66- .willReturn (
67- aResponse ()
68- .withStatus (503 )
69- .withHeader ("Content-Type" , "text/plain" )
70- .withBody ("Service Unavailable" )));
71- try {
72- metrics .getPodMetrics (namespace , "foo=bar" );
73- failBecauseExceptionWasNotThrown (ApiException .class );
74- } catch (ApiException ex ) {
75- assertThat (ex .getCode ()).isEqualTo (503 );
76- }
61+ get (urlPathMatching ("^/apis/metrics.k8s.io/v1beta1/namespaces/" + namespace + "/pods.*" ))
62+ .willReturn (
63+ aResponse ()
64+ .withStatus (503 )
65+ .withHeader ("Content-Type" , "text/plain" )
66+ .withBody ("Service Unavailable" )));
67+ assertThrows (ApiException .class , () -> metrics .getPodMetrics (namespace , "foo=bar" ));
7768 }
7869
7970 @ Test
@@ -86,11 +77,6 @@ void getNodeMetricsThrowsAPIExceptionWhenServerReturnsError() {
8677 .withStatus (503 )
8778 .withHeader ("Content-Type" , "text/plain" )
8879 .withBody ("Service Unavailable" )));
89- try {
90- metrics .getNodeMetrics ();
91- failBecauseExceptionWasNotThrown (ApiException .class );
92- } catch (ApiException ex ) {
93- assertThat (ex .getCode ()).isEqualTo (503 );
94- }
80+ assertThrows (ApiException .class , metrics ::getNodeMetrics );
9581 }
9682}
0 commit comments