Skip to content

Commit 2171559

Browse files
onobcCorneil du Plessis
authored and
Corneil du Plessis
committed
Update DockerCompose tests to Junit5
Also had to account for some changes in Mockito w/ varargs methods. Suggested changes from PR review
1 parent 4afe8d4 commit 2171559

File tree

3 files changed

+97
-134
lines changed

3 files changed

+97
-134
lines changed

spring-cloud-dataflow-common/spring-cloud-dataflow-common-test-docker/src/test/java/org/springframework/cloud/dataflow/common/test/docker/compose/execution/CommandTests.java

+27-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2019 the original author or authors.
2+
* Copyright 2018-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,88 +20,80 @@
2020
import java.util.List;
2121
import java.util.function.Consumer;
2222

23-
import org.junit.Before;
24-
import org.junit.Ignore;
25-
import org.junit.Test;
26-
import org.junit.runner.RunWith;
27-
import org.mockito.Mock;
28-
import org.mockito.junit.MockitoJUnitRunner;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Disabled;
25+
import org.junit.jupiter.api.Test;
2926

3027
import static org.apache.commons.io.IOUtils.toInputStream;
3128
import static org.hamcrest.MatcherAssert.assertThat;
3229
import static org.hamcrest.Matchers.contains;
3330
import static org.hamcrest.core.Is.is;
3431
import static org.mockito.ArgumentMatchers.any;
32+
import static org.mockito.Mockito.mock;
3533
import static org.mockito.Mockito.verify;
3634
import static org.mockito.Mockito.verifyNoMoreInteractions;
3735
import static org.mockito.Mockito.when;
3836

39-
@RunWith(MockitoJUnitRunner.class)
40-
public class CommandTests {
41-
@Mock private Process executedProcess;
42-
@Mock private DockerComposeExecutable dockerComposeExecutable;
43-
@Mock private ErrorHandler errorHandler;
37+
class CommandTests {
38+
39+
private Process executedProcess = mock(Process.class);
40+
private DockerComposeExecutable dockerComposeExecutable = mock(DockerComposeExecutable.class);
41+
private ErrorHandler errorHandler = mock(ErrorHandler.class);
4442
private Command dockerComposeCommand;
4543
private final List<String> consumedLogLines = new ArrayList<>();
4644
private final Consumer<String> logConsumer = s -> consumedLogLines.add(s);
4745

48-
@Before
49-
public void before() throws IOException {
46+
@BeforeEach
47+
void prepareForTest() throws IOException {
48+
when(dockerComposeExecutable.commandName()).thenReturn("docker-compose");
5049
when(dockerComposeExecutable.execute(any())).thenReturn(executedProcess);
50+
when(dockerComposeExecutable.execute(any(String[].class))).thenReturn(executedProcess);
5151
dockerComposeCommand = new Command(dockerComposeExecutable, logConsumer);
52-
5352
givenTheUnderlyingProcessHasOutput("");
5453
givenTheUnderlyingProcessTerminatesWithAnExitCodeOf(0);
5554
}
5655

57-
@Test public void
58-
invoke_error_handler_when_exit_code_of_the_executed_process_is_non_0() throws IOException, InterruptedException {
56+
@Test
57+
void invokeErrorHandlerWhenExitCodeOfTheExecutedProcessIsNonZero() throws Exception {
5958
int expectedExitCode = 1;
6059
givenTheUnderlyingProcessTerminatesWithAnExitCodeOf(expectedExitCode);
6160
dockerComposeCommand.execute(errorHandler, "rm", "-f");
62-
6361
verify(errorHandler).handle(expectedExitCode, "", "docker-compose", "rm", "-f");
6462
}
6563

66-
@Test public void
67-
not_invoke_error_handler_when_exit_code_of_the_executed_process_is_0() throws IOException, InterruptedException {
64+
@Test
65+
void notInvokeErrorHandlerWhenExitCodeOfTheExecutedProcessIsZero() throws Exception {
6866
dockerComposeCommand.execute(errorHandler, "rm", "-f");
69-
7067
verifyNoMoreInteractions(errorHandler);
7168
}
7269

73-
@Test public void
74-
return_output_when_exit_code_of_the_executed_process_is_non_0() throws IOException, InterruptedException {
70+
@Test
71+
void returnOutputWhenExitCodeOfTheExecutedProcessIsNonZero() throws Exception {
7572
String expectedOutput = "test output";
7673
givenTheUnderlyingProcessTerminatesWithAnExitCodeOf(1);
7774
givenTheUnderlyingProcessHasOutput(expectedOutput);
7875
String commandOutput = dockerComposeCommand.execute(errorHandler, "rm", "-f");
79-
8076
assertThat(commandOutput, is(expectedOutput));
8177
}
8278

83-
@Test public void
84-
return_output_when_exit_code_of_the_executed_process_is_0() throws IOException, InterruptedException {
79+
@Test
80+
void returnOutputWhenExitCodeOfTheExecutedProcessIsZero() throws Exception {
8581
String expectedOutput = "test output";
8682
givenTheUnderlyingProcessHasOutput(expectedOutput);
8783
String commandOutput = dockerComposeCommand.execute(errorHandler, "rm", "-f");
88-
8984
assertThat(commandOutput, is(expectedOutput));
9085
}
9186

92-
@Test public void
93-
give_the_output_to_the_specified_consumer_as_it_is_available() throws IOException, InterruptedException {
87+
@Test
88+
void giveTheOutputToTheSpecifiedConsumerAsItIsAvailable() throws Exception {
9489
givenTheUnderlyingProcessHasOutput("line 1\nline 2");
95-
9690
dockerComposeCommand.execute(errorHandler, "rm", "-f");
97-
9891
assertThat(consumedLogLines, contains("line 1", "line 2"));
9992
}
10093

101-
// flaky test: https://circleci.com/gh/palantir/docker-compose-rule/378, 370, 367, 366
102-
@Ignore
103-
@Test public void
104-
not_create_long_lived_threads_after_execution() throws IOException, InterruptedException {
94+
@Disabled("flaky test: https://circleci.com/gh/palantir/docker-compose-rule/378, 370, 367, 366")
95+
@Test
96+
void notCreateLongLivedThreadsAfterExecution() throws Exception {
10597
int preThreadCount = Thread.getAllStackTraces().entrySet().size();
10698
dockerComposeCommand.execute(errorHandler, "rm", "-f");
10799
int postThreadCount = Thread.getAllStackTraces().entrySet().size();

0 commit comments

Comments
 (0)