Skip to content

Commit d075315

Browse files
committed
mockito test
1 parent 07a7d57 commit d075315

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

docker-compose-junit-jupiter/src/main/java/com/palantir/docker/compose/DockerComposeExtension.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,24 @@
3030
public abstract class DockerComposeExtension extends DockerComposeManager
3131
implements BeforeAllCallback, AfterAllCallback {
3232

33+
private boolean hasCalledAfterMethod;
34+
3335
@Override
3436
public void beforeAll(ExtensionContext unused) throws IOException, InterruptedException {
3537
try {
3638
before();
3739
} catch (RuntimeException e) {
3840
after();
41+
hasCalledAfterMethod = true;
3942
throw e;
4043
}
4144
}
4245

4346
@Override
4447
public void afterAll(ExtensionContext unused) {
45-
after();
48+
if (!hasCalledAfterMethod) {
49+
after();
50+
}
4651
}
4752

4853
public static Builder builder() {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.palantir.docker.compose;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
21+
22+
import com.palantir.docker.compose.configuration.DockerComposeFiles;
23+
import com.palantir.docker.compose.connection.waiting.ClusterWait;
24+
import com.palantir.docker.compose.events.EventConsumer;
25+
import java.io.IOException;
26+
import java.util.List;
27+
import java.util.concurrent.atomic.AtomicInteger;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.extension.ExtensionContext;
30+
import org.mockito.Mockito;
31+
32+
public class DockerComposeExtensionTest {
33+
34+
@Test
35+
public void calls_after_only_once() throws IOException, InterruptedException {
36+
AtomicInteger count = new AtomicInteger();
37+
DockerComposeExtension dockerComposeExtension = new DockerComposeExtension() {
38+
39+
@Override
40+
public void before() {
41+
throw new IllegalStateException("some error");
42+
}
43+
44+
@Override
45+
public void after() {
46+
count.incrementAndGet();
47+
}
48+
49+
@Override
50+
public DockerComposeFiles files() {
51+
return null;
52+
}
53+
54+
@Override
55+
protected List<ClusterWait> clusterWaits() {
56+
return null;
57+
}
58+
59+
@Override
60+
protected List<EventConsumer> eventConsumers() {
61+
return null;
62+
}
63+
};
64+
65+
ExtensionContext extensionContext = Mockito.mock(ExtensionContext.class);
66+
assertThatThrownBy(() -> dockerComposeExtension.beforeAll(extensionContext))
67+
.isInstanceOf(IllegalStateException.class);
68+
dockerComposeExtension.afterAll(extensionContext);
69+
assertThat(count.get()).isEqualTo(1);
70+
}
71+
}

0 commit comments

Comments
 (0)