Skip to content

Commit 6195eb8

Browse files
tjleeKostyaSha
authored andcommitted
[tests] use expectedExceptions (docker-java#649)
1 parent 4c663e1 commit 6195eb8

30 files changed

+81
-205
lines changed

src/test/java/com/github/dockerjava/core/command/AuthCmdImplTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,9 @@ public void testAuth() throws Exception {
4545
}
4646

4747
// Disabled because of 500/InternalServerException
48-
@Test(enabled = false)
48+
@Test(enabled = false, expectedExceptions = UnauthorizedException.class, expectedExceptionsMessageRegExp = "Wrong login/password, please try again")
4949
public void testAuthInvalid() throws Exception {
5050

51-
try {
52-
DockerClientBuilder.getInstance(config("garbage")).build().authCmd().exec();
53-
fail("Expected a UnauthorizedException caused by a bad password.");
54-
} catch (UnauthorizedException e) {
55-
assertEquals(e.getMessage(), "Wrong login/password, please try again\n");
56-
}
51+
DockerClientBuilder.getInstance(config("garbage")).build().authCmd().exec();
5752
}
5853
}

src/test/java/com/github/dockerjava/core/command/CommitCmdImplTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,9 @@ public void commit() throws DockerException {
6868
assertThat(inspectImageResponse.getParent(), equalTo(busyboxImg.getId()));
6969
}
7070

71-
@Test
71+
@Test(expectedExceptions = NotFoundException.class)
7272
public void commitNonExistingContainer() throws DockerException {
73-
try {
74-
dockerClient.commitCmd("non-existent").exec();
75-
fail("expected NotFoundException");
76-
} catch (NotFoundException e) {
77-
}
78-
}
7973

74+
dockerClient.commitCmd("non-existent").exec();
75+
}
8076
}

src/test/java/com/github/dockerjava/core/command/ContainerDiffCmdImplTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,9 @@ public void testContainerDiff() throws DockerException {
6767
assertThat(testChangeLog, hasField("kind", equalTo(1)));
6868
}
6969

70-
@Test
70+
@Test(expectedExceptions = NotFoundException.class)
7171
public void testContainerDiffWithNonExistingContainer() throws DockerException {
72-
try {
73-
dockerClient.containerDiffCmd("non-existing").exec();
74-
fail("expected NotFoundException");
75-
} catch (NotFoundException e) {
76-
}
77-
}
7872

73+
dockerClient.containerDiffCmd("non-existing").exec();
74+
}
7975
}

src/test/java/com/github/dockerjava/core/command/CopyArchiveFromContainerCmdImplTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,10 @@ public void copyFromContainer() throws Exception {
7070
assertTrue(responseAsString.length() > 0);
7171
}
7272

73-
@Test
73+
@Test(expectedExceptions = NotFoundException.class)
7474
public void copyFromNonExistingContainer() throws Exception {
75-
try {
76-
dockerClient.copyArchiveFromContainerCmd("non-existing", "/test").exec();
77-
fail("expected NotFoundException");
78-
} catch (NotFoundException ignored) {
79-
}
75+
76+
dockerClient.copyArchiveFromContainerCmd("non-existing", "/test").exec();
8077
}
8178

8279
@Test

src/test/java/com/github/dockerjava/core/command/CopyArchiveToContainerCmdImplTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,10 @@ private void assertFileCopied(CreateContainerResponse container) throws IOExcept
8383
}
8484
}
8585

86-
@Test
86+
@Test(expectedExceptions = NotFoundException.class)
8787
public void copyToNonExistingContainer() throws Exception {
88-
try {
89-
dockerClient.copyArchiveToContainerCmd("non-existing").withHostResource("src/test/resources/testReadFile")
90-
.exec();
91-
fail("expected NotFoundException");
92-
} catch (NotFoundException ignored) {
93-
}
88+
89+
dockerClient.copyArchiveToContainerCmd("non-existing").withHostResource("src/test/resources/testReadFile").exec();
9490
}
9591

9692
@Test

src/test/java/com/github/dockerjava/core/command/CopyFileFromContainerCmdImplTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,9 @@ public void copyFromContainer() throws Exception {
6262
assertTrue(responseAsString.length() > 0);
6363
}
6464

65-
@Test
65+
@Test(expectedExceptions = NotFoundException.class)
6666
public void copyFromNonExistingContainer() throws Exception {
67-
try {
68-
dockerClient.copyFileFromContainerCmd("non-existing", "/test").exec();
69-
fail("expected NotFoundException");
70-
} catch (NotFoundException ignored) {
71-
}
67+
68+
dockerClient.copyFileFromContainerCmd("non-existing", "/test").exec();
7269
}
7370
}

src/test/java/com/github/dockerjava/core/command/CreateContainerCmdImplTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public void createContainerWithHostname() throws Exception {
231231
assertThat(containerLog(container.getId()), containsString("HOSTNAME=docker-java"));
232232
}
233233

234-
@Test
234+
@Test(expectedExceptions = ConflictException.class)
235235
public void createContainerWithName() throws DockerException {
236236

237237
CreateContainerResponse container = dockerClient.createContainerCmd(BUSYBOX_IMAGE).withName("container")
@@ -245,12 +245,8 @@ public void createContainerWithName() throws DockerException {
245245

246246
assertThat(inspectContainerResponse.getName(), equalTo("/container"));
247247

248-
try {
249-
dockerClient.createContainerCmd(BUSYBOX_IMAGE).withName("container").withCmd("env").exec();
250-
fail("Expected ConflictException");
251-
} catch (ConflictException e) {
252-
}
253248

249+
dockerClient.createContainerCmd(BUSYBOX_IMAGE).withName("container").withCmd("env").exec();
254250
}
255251

256252
@Test

src/test/java/com/github/dockerjava/core/command/KillContainerCmdImplTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,10 @@ public void killContainer() throws DockerException {
6767

6868
}
6969

70-
@Test
70+
@Test(expectedExceptions = NotFoundException.class)
7171
public void killNonExistingContainer() throws DockerException {
7272

73-
try {
74-
dockerClient.killContainerCmd("non-existing").exec();
75-
fail("expected NotFoundException");
76-
} catch (NotFoundException e) {
77-
}
73+
dockerClient.killContainerCmd("non-existing").exec();
7874
}
7975

8076
}

src/test/java/com/github/dockerjava/core/command/RemoveContainerCmdImplTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,10 @@ public void removeContainer() throws DockerException {
6868

6969
}
7070

71-
@Test
71+
@Test(expectedExceptions = NotFoundException.class)
7272
public void removeNonExistingContainer() throws DockerException {
73-
try {
74-
dockerClient.removeContainerCmd("non-existing").exec();
75-
fail("expected NotFoundException");
76-
} catch (NotFoundException e) {
77-
}
73+
74+
dockerClient.removeContainerCmd("non-existing").exec();
7875
}
7976

8077
}

src/test/java/com/github/dockerjava/core/command/RemoveImageCmdImplTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,10 @@ public void removeImage() throws DockerException, InterruptedException {
7474
assertThat(containers, matcher);
7575
}
7676

77-
@Test
77+
@Test(expectedExceptions = NotFoundException.class)
7878
public void removeNonExistingImage() throws DockerException, InterruptedException {
79-
try {
80-
dockerClient.removeImageCmd("non-existing").exec();
81-
fail("expected NotFoundException");
82-
} catch (NotFoundException e) {
83-
}
8479

80+
dockerClient.removeImageCmd("non-existing").exec();
8581
}
8682

8783
}

0 commit comments

Comments
 (0)