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

Handle AssumptionViolatedException in @Parameters method #1460

Merged
merged 8 commits into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
39 changes: 38 additions & 1 deletion src/main/java/org/junit/runners/Parameterized.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
import java.util.Collections;
import java.util.List;

import org.junit.internal.AssumptionViolatedException;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InvalidTestClassError;
import org.junit.runners.model.TestClass;
Expand Down Expand Up @@ -314,6 +318,29 @@ private void validatePublicStaticVoidMethods(
}
}

private static class AssumptionViolationRunner extends Runner {
private final Description description;
private final AssumptionViolatedException exception;

AssumptionViolationRunner(TestClass testClass, String methodName,
AssumptionViolatedException exception) {
this.description = Description
.createTestDescription(testClass.getJavaClass(),
methodName + "() assumption violation");
this.exception = exception;
}

@Override
public Description getDescription() {
return description;
}

@Override
public void run(RunNotifier notifier) {
notifier.fireTestAssumptionFailed(new Failure(description, exception));
}
}

private static class RunnersFactory {
private static final ParametersRunnerFactory DEFAULT_FACTORY = new BlockJUnit4ClassRunnerWithParametersFactory();

Expand All @@ -332,6 +359,10 @@ private RunnersFactory(Class<?> klass) throws Throwable {
}

private List<Runner> createRunners() throws Exception {
if (allParameters.size() == 1 && allParameters
.get(0) instanceof AssumptionViolationRunner) {
return Collections.singletonList((Runner) allParameters.get(0));
}
Parameters parameters = parametersMethod.getAnnotation(Parameters.class);
return Collections.unmodifiableList(createRunnersForParameters(
allParameters, parameters.name(),
Expand Down Expand Up @@ -365,7 +396,13 @@ private static Object[] normalizeParameters(Object parametersOrSingleParameter)
@SuppressWarnings("unchecked")
private static List<Object> allParameters(
TestClass testClass, FrameworkMethod parametersMethod) throws Throwable {
Object parameters = parametersMethod.invokeExplosively(null);
Object parameters;
try {
parameters = parametersMethod.invokeExplosively(null);
} catch (AssumptionViolatedException e) {
Copy link
Member

Choose a reason for hiding this comment

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

Couldn't you catch this exception in the caller (RunnersFactory)? If the exception gets caught, you can set a field in RunnersFactory named runnerOverride to a new AssumptionViolationRunner. If runnerOverride is set it would be returned from RunnersFactory.createRunners().

As a bonus, we could possibly catch any exception from there, and for the exceptions that aren't AssumptionViolatedException use ErrorReportingRunner.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've implemented the first part.

ErrorReportingRunner is already applied in RunnerBuilder.safeRunnerForClass(), seems like we should not care about other exceptions.

return Collections.<Object>singletonList(
new AssumptionViolationRunner(testClass, parametersMethod.getName(), e));
}
if (parameters instanceof List) {
return (List<Object>) parameters;
} else if (parameters instanceof Collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.junit.Assert.fail;
import static org.junit.experimental.results.PrintableResult.testResult;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -17,6 +18,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
Expand All @@ -27,6 +29,7 @@
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.runners.MethodSorters;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
Expand Down Expand Up @@ -763,4 +766,41 @@ public void usesParametersRunnerFactoryThatWasSpecifiedByAnnotationInSuperClass(
UseParameterizedFactoryTest.class,
"Called ExceptionThrowingRunnerFactory.");
}

@RunWith(Parameterized.class)
public static class ParameterizedAssumtionViolation {
@Parameters
public static Iterable<String> data() {
Assume.assumeTrue(false);
return Collections.singletonList("foobar");
}

public ParameterizedAssumtionViolation(String parameter) {
}

@Test
public void test1() {
}

@Test
public void test2() {
}
}

@Test
public void assumtionViolationInParameters() {
JUnitCore core = new JUnitCore();
final List<Failure> assumptionFailures = new ArrayList<Failure>();
core.addListener(new RunListener() {
@Override
public void testAssumptionFailure(Failure failure) {
assumptionFailures.add(failure);
}
});
Result result = core.run(ParameterizedAssumtionViolation.class);
assertTrue(result.wasSuccessful());
assertEquals(0, result.getRunCount());
assertEquals(0, result.getIgnoreCount());
assertEquals(1, assumptionFailures.size());
}
}