-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a JUnit 4 rule to skip tests #6445
Open
jtnord
wants to merge
1
commit into
testcontainers:main
Choose a base branch
from
jtnord:require-docker-rule
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+89
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
core/src/main/java/org/testcontainers/RequireContainerSupportRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,38 @@ | ||||||||
package org.testcontainers; | ||||||||
|
||||||||
import org.junit.AssumptionViolatedException; | ||||||||
import org.junit.Rule; | ||||||||
import org.junit.rules.TestRule; | ||||||||
import org.junit.runner.Description; | ||||||||
import org.junit.runners.model.Statement; | ||||||||
|
||||||||
/** | ||||||||
* Junit 4 {@code Rule} that will skip the test if the docker client is not available. | ||||||||
* This rule must have a lower {@link Rule#order} specified that any other {@code Rule} that | ||||||||
* uses docker in order to skip tests, otherwise the other rules would fail first causing build failures. | ||||||||
* e.g. <pre><code> | ||||||||
{@literal @}Rule(order = -10) | ||||||||
public RequireContainerSupportRule rcr = new RequireContainerSupportRule(); | ||||||||
} | ||||||||
* </code></pre> | ||||||||
*/ | ||||||||
public class RequireContainerSupportRule implements TestRule { | ||||||||
|
||||||||
@Override | ||||||||
public Statement apply(Statement base, Description description) { | ||||||||
if (DockerClientFactory.instance().isDockerAvailable()) { | ||||||||
return base; | ||||||||
} | ||||||||
return new DockerNotAvailbleStatement(); | ||||||||
} | ||||||||
|
||||||||
private static class DockerNotAvailbleStatement extends Statement { | ||||||||
|
||||||||
@Override | ||||||||
public void evaluate() throws Throwable { | ||||||||
throw new AssumptionViolatedException( | ||||||||
"Docker support is not available and this test requires TestContainers which needs docker" | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
); | ||||||||
} | ||||||||
} | ||||||||
} |
51 changes: 51 additions & 0 deletions
51
core/src/test/java/org/testcontainers/RequireContainerSupportRuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.testcontainers; | ||
|
||
import org.junit.AssumptionViolatedException; | ||
import org.junit.Test; | ||
import org.junit.runners.model.Statement; | ||
import org.mockito.MockedStatic; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class RequireContainerSupportRuleTest { | ||
|
||
@Test | ||
public void assumptionViolationisThrownWithoutDockerSpport() { | ||
try (MockedStatic<DockerClientFactory> staticallyMockedFactory = mockStatic(DockerClientFactory.class)) { | ||
DockerClientFactory mockedFactory = mock(DockerClientFactory.class); | ||
when(mockedFactory.isDockerAvailable()).thenReturn(false); | ||
staticallyMockedFactory.when(DockerClientFactory::instance).thenReturn(mockedFactory); | ||
|
||
RequireContainerSupportRule rcsr = new RequireContainerSupportRule(); | ||
|
||
assertThatThrownBy(() -> rcsr.apply(new EmptyStatement(), null).evaluate()) | ||
.isInstanceOf(AssumptionViolatedException.class) | ||
.hasMessage("Docker support is not available and this test requires TestContainers which needs docker"); | ||
} | ||
} | ||
|
||
@Test | ||
public void assumptionViolationisNotThrownWithDockerSpport() throws Throwable { | ||
try (MockedStatic<DockerClientFactory> staticallyMockedFactory = mockStatic(DockerClientFactory.class)) { | ||
DockerClientFactory mockedFactory = mock(DockerClientFactory.class); | ||
when(mockedFactory.isDockerAvailable()).thenReturn(true); | ||
staticallyMockedFactory.when(DockerClientFactory::instance).thenReturn(mockedFactory); | ||
|
||
RequireContainerSupportRule rcsr = new RequireContainerSupportRule(); | ||
|
||
// any exception thrown will ripple out and fail the test | ||
rcsr.apply(new EmptyStatement(), null).evaluate(); | ||
} | ||
} | ||
|
||
private static class EmptyStatement extends Statement { | ||
|
||
@Override | ||
public void evaluate() throws Throwable { | ||
// no op. | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went round the houses on the name of this a few times - in reality not sure if you require
docker
(the cli) at all, or just a configuration for docker (which could come from podman etc.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use docker, similar to what we have for Testcontainers annotation for JUnit Jupiter. See
testcontainers-java/modules/junit-jupiter/src/main/java/org/testcontainers/junit/jupiter/Testcontainers.java
Line 65 in de1324e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if
DockerCheckRule
make sense. I know naming is hard :)