Skip to content

Commit

Permalink
Handle AssumptionViolatedException in @parameters method (junit-team#…
Browse files Browse the repository at this point in the history
  • Loading branch information
panchenko authored and kcooney committed Jun 14, 2017
1 parent 2cb24ea commit 7111b96
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
44 changes: 42 additions & 2 deletions 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,24 +318,60 @@ 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();

private final TestClass testClass;
private final FrameworkMethod parametersMethod;
private final List<Object> allParameters;
private final int parameterCount;

private final Runner runnerOverride;

private RunnersFactory(Class<?> klass) throws Throwable {
testClass = new TestClass(klass);
parametersMethod = getParametersMethod(testClass);
allParameters = allParameters(testClass, parametersMethod);
List<Object> allParametersResult;
AssumptionViolationRunner assumptionViolationRunner = null;
try {
allParametersResult = allParameters(testClass, parametersMethod);
} catch (AssumptionViolatedException e) {
allParametersResult = Collections.emptyList();
assumptionViolationRunner = new AssumptionViolationRunner(testClass,
parametersMethod.getName(), e);
}
allParameters = allParametersResult;
runnerOverride = assumptionViolationRunner;
parameterCount =
allParameters.isEmpty() ? 0 : normalizeParameters(allParameters.get(0)).length;
}

private List<Runner> createRunners() throws Exception {
if (runnerOverride != null) {
return Collections.singletonList(runnerOverride);
}
Parameters parameters = parametersMethod.getAnnotation(Parameters.class);
return Collections.unmodifiableList(createRunnersForParameters(
allParameters, parameters.name(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
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 @@ -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,49 @@ public void usesParametersRunnerFactoryThatWasSpecifiedByAnnotationInSuperClass(
UseParameterizedFactoryTest.class,
"Called ExceptionThrowingRunnerFactory.");
}

@RunWith(Parameterized.class)
public static class ParameterizedAssumtionViolation {
static boolean condition;

@Parameters
public static Iterable<String> data() {
assumeTrue(condition);
return Collections.singletonList("foobar");
}

public ParameterizedAssumtionViolation(String parameter) {
}

@Test
public void test1() {
}

@Test
public void test2() {
}
}

@Test
public void assumtionViolationInParameters() {
ParameterizedAssumtionViolation.condition = true;
Result successResult = JUnitCore.runClasses(ParameterizedAssumtionViolation.class);
assertTrue(successResult.wasSuccessful());
assertEquals(2, successResult.getRunCount());

ParameterizedAssumtionViolation.condition = false;
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 failureResult = core.run(ParameterizedAssumtionViolation.class);
assertTrue(failureResult.wasSuccessful());
assertEquals(0, failureResult.getRunCount());
assertEquals(0, failureResult.getIgnoreCount());
assertEquals(1, assumptionFailures.size());
}
}

0 comments on commit 7111b96

Please sign in to comment.