Skip to content

Commit b7041d0

Browse files
Adding a negative unit test
1 parent 38b5cf4 commit b7041d0

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

util/src/main/java/io/kubernetes/client/util/version/Version.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public VersionInfo getVersion() throws ApiException, IOException {
3838
Response response = null;
3939
try {
4040
response = call.execute();
41-
} catch (IOException ex) {
42-
throw new ApiException(ex);
41+
} catch (IOException e) {
42+
throw new ApiException(e);
4343
}
4444

4545
if (!response.isSuccessful()) {

util/src/test/java/io/kubernetes/client/util/version/VersionTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
1919
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
2020
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
21+
import static org.junit.Assert.assertEquals;
2122

2223
import com.github.tomakehurst.wiremock.junit.WireMockRule;
2324
import io.kubernetes.client.openapi.ApiClient;
@@ -61,6 +62,34 @@ public void testUrl() throws InterruptedException, IOException, ApiException {
6162

6263
verify(
6364
getRequestedFor(urlPathEqualTo("/version/"))
64-
.withHeader("Content-Type", equalTo("application/json")));
65+
.withHeader("Content-Type", equalTo("application/json"))
66+
.withHeader("Accept", equalTo("application/json")));
67+
}
68+
69+
@Test
70+
public void testFailure() throws InterruptedException, IOException, ApiException {
71+
72+
wireMockRule.stubFor(
73+
get(urlPathEqualTo("/version/"))
74+
.willReturn(
75+
aResponse()
76+
.withStatus(401)
77+
.withHeader("Content-Type", "application/json")
78+
.withBody("{}")));
79+
80+
Version versionUtil = new Version(client);
81+
boolean thrown = false;
82+
try {
83+
VersionInfo versionInfo = versionUtil.getVersion();
84+
} catch (ApiException ex) {
85+
assertEquals(401, ex.getCode());
86+
thrown = true;
87+
}
88+
assertEquals(thrown, true);
89+
90+
verify(
91+
getRequestedFor(urlPathEqualTo("/version/"))
92+
.withHeader("Content-Type", equalTo("application/json"))
93+
.withHeader("Accept", equalTo("application/json")));
6594
}
6695
}

0 commit comments

Comments
 (0)