Skip to content

Commit 92a3f73

Browse files
author
David Saff
committed
Merge pull request #601 from skazzyy/master
Fix for issue #499 Assumes in tests run by Theories
2 parents 89b4004 + 178f854 commit 92a3f73

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/main/java/org/junit/experimental/theories/Theories.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ private TestClass getTestClass() {
9696
public void evaluate() throws Throwable {
9797
runWithAssignment(Assignments.allUnassigned(
9898
fTestMethod.getMethod(), getTestClass()));
99-
100-
if (successes == 0) {
99+
100+
//if this test method is not annotated with Theory, then no successes is a valid case
101+
boolean hasTheoryAnnotation = fTestMethod.getAnnotation(Theory.class) != null;
102+
if (successes == 0 && hasTheoryAnnotation) {
101103
Assert
102104
.fail("Never found parameters that satisfied method assumptions. Violated assumptions: "
103105
+ fInvalidParameters);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.junit.tests.experimental.theories;
2+
3+
import org.junit.Assert;
4+
import org.junit.Assume;
5+
import org.junit.Test;
6+
import org.junit.experimental.theories.DataPoint;
7+
import org.junit.experimental.theories.Theories;
8+
import org.junit.experimental.theories.Theory;
9+
import org.junit.runner.JUnitCore;
10+
import org.junit.runner.Request;
11+
import org.junit.runner.Result;
12+
import org.junit.runner.RunWith;
13+
import org.junit.runner.Runner;
14+
import org.junit.runners.model.InitializationError;
15+
16+
@RunWith(Theories.class)
17+
public class AssumingInTheoriesTest {
18+
19+
@Test
20+
public void noTheoryAnnotationMeansAssumeShouldIgnore() {
21+
Assume.assumeTrue(false);
22+
}
23+
24+
@Test
25+
public void theoryMeansOnlyAssumeShouldFail() throws InitializationError {
26+
JUnitCore junitRunner = new JUnitCore();
27+
Runner theoryRunner = new Theories(TheoryWithNoUnassumedParameters.class);
28+
Request request = Request.runner(theoryRunner);
29+
Result result = junitRunner.run(request);
30+
Assert.assertEquals(1, result.getFailureCount());
31+
}
32+
33+
/**
34+
* Simple class that SHOULD fail because no parameters are met.
35+
*/
36+
public static class TheoryWithNoUnassumedParameters {
37+
38+
@DataPoint
39+
public final static boolean FALSE = false;
40+
41+
@Theory
42+
public void theoryWithNoUnassumedParameters(boolean value) {
43+
Assume.assumeTrue(value);
44+
}
45+
}
46+
47+
}

0 commit comments

Comments
 (0)