Skip to content

Commit

Permalink
Handle AssumptionViolatedException in @parameters method
Browse files Browse the repository at this point in the history
  • Loading branch information
panchenko committed May 25, 2017
1 parent 9ee3814 commit 8a50082
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
37 changes: 36 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,27 @@ 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;
}

public Description getDescription() {
return description;
}

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 +357,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 +394,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) {
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 @@ -17,6 +17,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 Down Expand Up @@ -763,4 +764,31 @@ 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() {
Result result = JUnitCore.runClasses(ParameterizedAssumtionViolation.class);
assertEquals(0, result.getRunCount());
assertEquals(1, result.getIgnoreCount());
}
}

0 comments on commit 8a50082

Please sign in to comment.