Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
mockito test
  • Loading branch information
pnepywoda committed Nov 14, 2019
commit 5405420f8e5afb280273a18dd62b857a128a2c39
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,24 @@
public abstract class DockerComposeExtension extends DockerComposeManager
implements BeforeAllCallback, AfterAllCallback {

private boolean hasCalledAfterMethod;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should live in DockerComposeManager, not DockerComposeExtension that way DockerComposeRule gets this behaviour too.


@Override
public void beforeAll(ExtensionContext unused) throws IOException, InterruptedException {
try {
before();
} catch (RuntimeException e) {
after();
hasCalledAfterMethod = true;
throw e;
}
}

@Override
public void afterAll(ExtensionContext unused) {
after();
if (!hasCalledAfterMethod) {
after();
}
}

public static Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.docker.compose;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.palantir.docker.compose.configuration.DockerComposeFiles;
import com.palantir.docker.compose.connection.waiting.ClusterWait;
import com.palantir.docker.compose.events.EventConsumer;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.mockito.Mockito;

public class DockerComposeExtensionTest {

@Test
public void calls_after_only_once() throws IOException, InterruptedException {
AtomicInteger count = new AtomicInteger();
DockerComposeExtension dockerComposeExtension = new DockerComposeExtension() {

@Override
public void before() {
throw new IllegalStateException("some error");
}

@Override
public void after() {
count.incrementAndGet();
}

@Override
public DockerComposeFiles files() {
return null;
}

@Override
protected List<ClusterWait> clusterWaits() {
return null;
}

@Override
protected List<EventConsumer> eventConsumers() {
return null;
}
};

ExtensionContext extensionContext = Mockito.mock(ExtensionContext.class);
assertThatThrownBy(() -> dockerComposeExtension.beforeAll(extensionContext))
.isInstanceOf(IllegalStateException.class);
dockerComposeExtension.afterAll(extensionContext);
assertThat(count.get()).isEqualTo(1);
}
}