Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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 {
Copy link
Author

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.)

Copy link
Member

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

Copy link
Member

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 :)


@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"
Copy link
Author

Choose a reason for hiding this comment

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

could be Docker support... or Container support...

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"Docker support is not available and this test requires TestContainers which needs docker"
"Docker is not available"

);
}
}
}
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.
}
}
}