diff --git a/src/main/java/org/junit/experimental/theories/DataPoint.java b/src/main/java/org/junit/experimental/theories/DataPoint.java index bb047fecd2fe..0a017bb5631e 100644 --- a/src/main/java/org/junit/experimental/theories/DataPoint.java +++ b/src/main/java/org/junit/experimental/theories/DataPoint.java @@ -52,4 +52,5 @@ @Target({FIELD, METHOD}) public @interface DataPoint { String[] value() default {}; -} + Class[] ignoredExceptions() default {}; +} \ No newline at end of file diff --git a/src/main/java/org/junit/experimental/theories/DataPoints.java b/src/main/java/org/junit/experimental/theories/DataPoints.java index 68a3c9e650d9..61d46030f662 100644 --- a/src/main/java/org/junit/experimental/theories/DataPoints.java +++ b/src/main/java/org/junit/experimental/theories/DataPoints.java @@ -51,4 +51,5 @@ @Target({FIELD, METHOD}) public @interface DataPoints { String[] value() default {}; + Class[] ignoredExceptions() default {}; } diff --git a/src/main/java/org/junit/experimental/theories/ParameterSupplier.java b/src/main/java/org/junit/experimental/theories/ParameterSupplier.java index c2981ee01b10..11916b31699e 100644 --- a/src/main/java/org/junit/experimental/theories/ParameterSupplier.java +++ b/src/main/java/org/junit/experimental/theories/ParameterSupplier.java @@ -3,5 +3,5 @@ import java.util.List; public abstract class ParameterSupplier { - public abstract List getValueSources(ParameterSignature sig); + public abstract List getValueSources(ParameterSignature sig) throws Throwable; } diff --git a/src/main/java/org/junit/experimental/theories/PotentialAssignment.java b/src/main/java/org/junit/experimental/theories/PotentialAssignment.java index d4e401062ec6..f25fda07e60b 100644 --- a/src/main/java/org/junit/experimental/theories/PotentialAssignment.java +++ b/src/main/java/org/junit/experimental/theories/PotentialAssignment.java @@ -5,6 +5,13 @@ public abstract class PotentialAssignment { public static class CouldNotGenerateValueException extends Exception { private static final long serialVersionUID = 1L; + + public CouldNotGenerateValueException() { + } + + public CouldNotGenerateValueException(Throwable t) { + super(t); + } } public static PotentialAssignment forValue(final String name, final Object value) { diff --git a/src/main/java/org/junit/experimental/theories/Theories.java b/src/main/java/org/junit/experimental/theories/Theories.java index b4d8a28052f2..056ee1788f3e 100644 --- a/src/main/java/org/junit/experimental/theories/Theories.java +++ b/src/main/java/org/junit/experimental/theories/Theories.java @@ -9,7 +9,7 @@ import java.util.List; import org.junit.Assert; -import org.junit.experimental.theories.PotentialAssignment.CouldNotGenerateValueException; +import org.junit.Assume; import org.junit.experimental.theories.internal.Assignments; import org.junit.experimental.theories.internal.ParameterizedAssertionError; import org.junit.internal.AssumptionViolatedException; @@ -202,8 +202,13 @@ protected Statement methodInvoker(FrameworkMethod method, Object test) { @Override public Object createTest() throws Exception { - return getTestClass().getOnlyConstructor().newInstance( - complete.getConstructorArguments(nullsOk())); + Object[] params = complete.getConstructorArguments(); + + if (!nullsOk()) { + Assume.assumeNotNull(params); + } + + return getTestClass().getOnlyConstructor().newInstance(params); } }.methodBlock(fTestMethod).evaluate(); } @@ -213,13 +218,13 @@ private Statement methodCompletesWithParameters( return new Statement() { @Override public void evaluate() throws Throwable { - try { - final Object[] values = complete.getMethodArguments( - nullsOk()); - method.invokeExplosively(freshInstance, values); - } catch (CouldNotGenerateValueException e) { - // ignore + final Object[] values = complete.getMethodArguments(); + + if (!nullsOk()) { + Assume.assumeNotNull(values); } + + method.invokeExplosively(freshInstance, values); } }; } diff --git a/src/main/java/org/junit/experimental/theories/internal/AllMembersSupplier.java b/src/main/java/org/junit/experimental/theories/internal/AllMembersSupplier.java index 1f6590cc5bb5..a8d7c9a6bd9d 100644 --- a/src/main/java/org/junit/experimental/theories/internal/AllMembersSupplier.java +++ b/src/main/java/org/junit/experimental/theories/internal/AllMembersSupplier.java @@ -6,6 +6,7 @@ import java.util.Collection; import java.util.List; +import org.junit.Assume; import org.junit.experimental.theories.DataPoint; import org.junit.experimental.theories.DataPoints; import org.junit.experimental.theories.ParameterSignature; @@ -36,17 +37,20 @@ public Object getValue() throws CouldNotGenerateValueException { } catch (IllegalAccessException e) { throw new RuntimeException( "unexpected: getMethods returned an inaccessible method"); - } catch (Throwable e) { - throw new CouldNotGenerateValueException(); - // do nothing, just look for more values + } catch (Throwable throwable) { + DataPoint annotation = fMethod.getAnnotation(DataPoint.class); + Assume.assumeTrue(annotation == null || !isAssignableToAnyOf(annotation.ignoredExceptions(), throwable)); + + throw new CouldNotGenerateValueException(throwable); } - } - + } + @Override public String getDescription() throws CouldNotGenerateValueException { return fMethod.getName(); } - } + } + private final TestClass fClass; /** @@ -57,7 +61,7 @@ public AllMembersSupplier(TestClass type) { } @Override - public List getValueSources(ParameterSignature sig) { + public List getValueSources(ParameterSignature sig) throws Throwable { List list = new ArrayList(); addSinglePointFields(sig, list); @@ -68,15 +72,20 @@ public List getValueSources(ParameterSignature sig) { return list; } - private void addMultiPointMethods(ParameterSignature sig, List list) { + private void addMultiPointMethods(ParameterSignature sig, List list) throws Throwable { for (FrameworkMethod dataPointsMethod : getDataPointsMethods(sig)) { Class returnType = dataPointsMethod.getReturnType(); if (returnType.isArray() && sig.canPotentiallyAcceptType(returnType.getComponentType())) { try { addArrayValues(sig, dataPointsMethod.getName(), list, dataPointsMethod.invokeExplosively(null)); - } catch (Throwable e) { - // ignore and move on + } catch (Throwable throwable) { + DataPoints annotation = dataPointsMethod.getAnnotation(DataPoints.class); + if (annotation != null && isAssignableToAnyOf(annotation.ignoredExceptions(), throwable)) { + return; + } else { + throw throwable; + } } } } @@ -94,7 +103,7 @@ private void addMultiPointFields(ParameterSignature sig, List list) { for (final Field field : getSingleDataPointFields(sig)) { @@ -126,6 +135,15 @@ private Object getStaticFieldValue(final Field field) { "unexpected: getFields returned an inaccessible field"); } } + + private static boolean isAssignableToAnyOf(Class[] typeArray, Object target) { + for (Class type : typeArray) { + if (type.isAssignableFrom(target.getClass())) { + return true; + } + } + return false; + } protected Collection getDataPointsMethods(ParameterSignature sig) { return fClass.getAnnotatedMethods(DataPoints.class); diff --git a/src/main/java/org/junit/experimental/theories/internal/Assignments.java b/src/main/java/org/junit/experimental/theories/internal/Assignments.java index c30aff38bd34..0baaa52baf2b 100644 --- a/src/main/java/org/junit/experimental/theories/internal/Assignments.java +++ b/src/main/java/org/junit/experimental/theories/internal/Assignments.java @@ -63,21 +63,17 @@ public Assignments assignNext(PotentialAssignment source) { fUnassigned.size()), fClass); } - public Object[] getActualValues(int start, int stop, boolean nullsOk) + public Object[] getActualValues(int start, int stop) throws CouldNotGenerateValueException { Object[] values = new Object[stop - start]; for (int i = start; i < stop; i++) { - Object value = fAssigned.get(i).getValue(); - if (value == null && !nullsOk) { - throw new CouldNotGenerateValueException(); - } - values[i - start] = value; + values[i - start] = fAssigned.get(i).getValue(); } return values; } public List potentialsForNextUnassigned() - throws Exception { + throws Throwable { ParameterSignature unassigned = nextUnassigned(); List assignments = getSupplier(unassigned).getValueSources(unassigned); @@ -127,20 +123,17 @@ private ParameterSupplier buildParameterSupplierFromClass( return cls.newInstance(); } - public Object[] getConstructorArguments(boolean nullsOk) + public Object[] getConstructorArguments() throws CouldNotGenerateValueException { - return getActualValues(0, getConstructorParameterCount(), nullsOk); + return getActualValues(0, getConstructorParameterCount()); } - public Object[] getMethodArguments(boolean nullsOk) - throws CouldNotGenerateValueException { - return getActualValues(getConstructorParameterCount(), - fAssigned.size(), nullsOk); + public Object[] getMethodArguments() throws CouldNotGenerateValueException { + return getActualValues(getConstructorParameterCount(), fAssigned.size()); } - public Object[] getAllArguments(boolean nullsOk) - throws CouldNotGenerateValueException { - return getActualValues(0, fAssigned.size(), nullsOk); + public Object[] getAllArguments() throws CouldNotGenerateValueException { + return getActualValues(0, fAssigned.size()); } private int getConstructorParameterCount() { diff --git a/src/main/java/org/junit/experimental/theories/internal/SpecificDataPointsSupplier.java b/src/main/java/org/junit/experimental/theories/internal/SpecificDataPointsSupplier.java index cd18292efa57..7b571e335941 100644 --- a/src/main/java/org/junit/experimental/theories/internal/SpecificDataPointsSupplier.java +++ b/src/main/java/org/junit/experimental/theories/internal/SpecificDataPointsSupplier.java @@ -33,7 +33,7 @@ protected Collection getSingleDataPointFields(ParameterSignature sig) { } } - return fieldsWithMatchingNames; + return fieldsWithMatchingNames; } @Override diff --git a/src/test/java/org/junit/tests/experimental/theories/TheoryTestUtils.java b/src/test/java/org/junit/tests/experimental/theories/TheoryTestUtils.java index 7320bbf6e8e0..6e1b35165cc2 100644 --- a/src/test/java/org/junit/tests/experimental/theories/TheoryTestUtils.java +++ b/src/test/java/org/junit/tests/experimental/theories/TheoryTestUtils.java @@ -18,7 +18,7 @@ public final class TheoryTestUtils { private TheoryTestUtils() { } public static List potentialAssignments(Method method) - throws Exception { + throws Throwable { return Assignments.allUnassigned(method, new TestClass(method.getDeclaringClass())) .potentialsForNextUnassigned(); diff --git a/src/test/java/org/junit/tests/experimental/theories/extendingwithstubs/StubbedTheories.java b/src/test/java/org/junit/tests/experimental/theories/extendingwithstubs/StubbedTheories.java index 5dafd744d735..1b68da858c5a 100644 --- a/src/test/java/org/junit/tests/experimental/theories/extendingwithstubs/StubbedTheories.java +++ b/src/test/java/org/junit/tests/experimental/theories/extendingwithstubs/StubbedTheories.java @@ -50,7 +50,7 @@ protected void runWithIncompleteAssignment(Assignments incomplete) } private GuesserQueue createGuesserQueue(Assignments incomplete) - throws Exception { + throws Throwable { ParameterSignature nextUnassigned = incomplete.nextUnassigned(); if (nextUnassigned.hasAnnotation(Stub.class)) { diff --git a/src/test/java/org/junit/tests/experimental/theories/internal/AllMembersSupplierTest.java b/src/test/java/org/junit/tests/experimental/theories/internal/AllMembersSupplierTest.java index 1374aa8df5ae..eac9f2d9ed0d 100644 --- a/src/test/java/org/junit/tests/experimental/theories/internal/AllMembersSupplierTest.java +++ b/src/test/java/org/junit/tests/experimental/theories/internal/AllMembersSupplierTest.java @@ -8,6 +8,7 @@ import java.util.List; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.theories.DataPoint; import org.junit.experimental.theories.DataPoints; @@ -15,9 +16,13 @@ import org.junit.experimental.theories.PotentialAssignment; import org.junit.experimental.theories.Theory; import org.junit.experimental.theories.internal.AllMembersSupplier; +import org.junit.rules.ExpectedException; import org.junit.runners.model.TestClass; public class AllMembersSupplierTest { + @Rule + public ExpectedException expected = ExpectedException.none(); + public static class HasDataPointsArrayField { @DataPoints @@ -29,7 +34,7 @@ public void theory(String param) { } @Test - public void dataPointsArrayShouldBeRecognized() throws Exception { + public void dataPointsArrayShouldBeRecognized() throws Throwable { List assignments = potentialAssignments( HasDataPointsArrayField.class.getMethod("theory", String.class)); @@ -46,7 +51,7 @@ public void theory(Integer param) { } @Test - public void dataPointsArrayShouldBeRecognizedOnValueTypeNotFieldType() throws Exception { + public void dataPointsArrayShouldBeRecognizedOnValueTypeNotFieldType() throws Throwable { List assignments = potentialAssignments( HasDataPointsArrayWithMatchingButInaccurateTypes.class.getMethod("theory", Integer.class)); @@ -65,7 +70,7 @@ public void theory(Object param) { } @Test - public void dataPointMethodShouldBeRecognizedForOverlyGeneralParameters() throws Exception { + public void dataPointMethodShouldBeRecognizedForOverlyGeneralParameters() throws Throwable { List assignments = potentialAssignments( HasDataPointMethodWithOverlyGeneralTypes.class.getMethod("theory", Object.class)); @@ -82,7 +87,7 @@ public void theory(Object obj) { } @Test - public void dataPointsAnnotationMeansTreatAsArrayOnly() throws Exception { + public void dataPointsAnnotationMeansTreatAsArrayOnly() throws Throwable { List assignments = potentialAssignments( HasDataPointsWithObjectParameter.class.getMethod("theory", Object.class)); @@ -101,13 +106,9 @@ public HasDataPointsFieldWithNullValue(Object obj) { } @Test - public void dataPointsArrayFieldMayContainNullValue() - throws SecurityException, NoSuchMethodException { - List valueSources = new AllMembersSupplier( - new TestClass(HasDataPointsFieldWithNullValue.class)) - .getValueSources(ParameterSignature.signatures( - HasDataPointsFieldWithNullValue.class.getConstructor(Object.class)) - .get(0)); + public void dataPointsArrayFieldMayContainNullValue() throws Throwable { + List valueSources = allMemberValuesFor( + HasDataPointsFieldWithNullValue.class, Object.class); assertThat(valueSources.size(), is(2)); } @@ -122,13 +123,34 @@ public HasDataPointsMethodWithNullValue(Integer i) { } @Test - public void dataPointsArrayMethodMayContainNullValue() - throws SecurityException, NoSuchMethodException { - List valueSources = new AllMembersSupplier( - new TestClass(HasDataPointsMethodWithNullValue.class)) + public void dataPointsArrayMethodMayContainNullValue() throws Throwable { + List valueSources = allMemberValuesFor( + HasDataPointsMethodWithNullValue.class, Integer.class); + assertThat(valueSources.size(), is(2)); + } + + public static class HasFailingDataPointsArrayMethod { + @DataPoints + public static Object[] objects() { + throw new RuntimeException("failing method"); + } + + public HasFailingDataPointsArrayMethod(Object obj) { + } + } + + @Test + public void allMembersFailsOnFailingDataPointsArrayMethod() throws Throwable { + expected.expect(RuntimeException.class); + expected.expectMessage("failing method"); + allMemberValuesFor(HasFailingDataPointsArrayMethod.class, Object.class); + } + + private List allMemberValuesFor(Class testClass, + Class... constructorParameterTypes) throws Throwable { + return new AllMembersSupplier(new TestClass(testClass)) .getValueSources(ParameterSignature.signatures( - HasDataPointsMethodWithNullValue.class.getConstructor(Integer.class)) + testClass.getConstructor(constructorParameterTypes)) .get(0)); - assertThat(valueSources.size(), is(2)); } } diff --git a/src/test/java/org/junit/tests/experimental/theories/internal/SpecificDataPointsSupplierTest.java b/src/test/java/org/junit/tests/experimental/theories/internal/SpecificDataPointsSupplierTest.java index 3e668c7b58dd..3ccf902c9bcf 100644 --- a/src/test/java/org/junit/tests/experimental/theories/internal/SpecificDataPointsSupplierTest.java +++ b/src/test/java/org/junit/tests/experimental/theories/internal/SpecificDataPointsSupplierTest.java @@ -56,7 +56,7 @@ public static String[] getOtherValues() { } @Test - public void shouldReturnOnlyTheNamedDataPoints() throws Exception { + public void shouldReturnOnlyTheNamedDataPoints() throws Throwable { SpecificDataPointsSupplier supplier = new SpecificDataPointsSupplier(new TestClass(TestClassWithNamedDataPoints.class)); List assignments = supplier.getValueSources(signature("methodWantingAllNamedStrings")); @@ -67,7 +67,7 @@ public void shouldReturnOnlyTheNamedDataPoints() throws Exception { } @Test - public void shouldReturnOnlyTheNamedFieldDataPoints() throws Exception { + public void shouldReturnOnlyTheNamedFieldDataPoints() throws Throwable { SpecificDataPointsSupplier supplier = new SpecificDataPointsSupplier(new TestClass(TestClassWithNamedDataPoints.class)); List assignments = supplier.getValueSources(signature("methodWantingNamedFieldString")); @@ -78,7 +78,7 @@ public void shouldReturnOnlyTheNamedFieldDataPoints() throws Exception { } @Test - public void shouldReturnOnlyTheNamedMethodDataPoints() throws Exception { + public void shouldReturnOnlyTheNamedMethodDataPoints() throws Throwable { SpecificDataPointsSupplier supplier = new SpecificDataPointsSupplier(new TestClass(TestClassWithNamedDataPoints.class)); List assignments = supplier.getValueSources(signature("methodWantingNamedMethodString")); @@ -89,7 +89,7 @@ public void shouldReturnOnlyTheNamedMethodDataPoints() throws Exception { } @Test - public void shouldReturnOnlyTheNamedSingleFieldDataPoints() throws Exception { + public void shouldReturnOnlyTheNamedSingleFieldDataPoints() throws Throwable { SpecificDataPointsSupplier supplier = new SpecificDataPointsSupplier(new TestClass(TestClassWithNamedDataPoints.class)); List assignments = supplier.getValueSources(signature("methodWantingNamedSingleFieldString")); @@ -100,7 +100,7 @@ public void shouldReturnOnlyTheNamedSingleFieldDataPoints() throws Exception { } @Test - public void shouldReturnOnlyTheNamedSingleMethodDataPoints() throws Exception { + public void shouldReturnOnlyTheNamedSingleMethodDataPoints() throws Throwable { SpecificDataPointsSupplier supplier = new SpecificDataPointsSupplier(new TestClass(TestClassWithNamedDataPoints.class)); List assignments = supplier.getValueSources(signature("methodWantingNamedSingleMethodString")); @@ -111,7 +111,7 @@ public void shouldReturnOnlyTheNamedSingleMethodDataPoints() throws Exception { } @Test - public void shouldReturnNothingIfTheNamedDataPointsAreMissing() throws Exception { + public void shouldReturnNothingIfTheNamedDataPointsAreMissing() throws Throwable { SpecificDataPointsSupplier supplier = new SpecificDataPointsSupplier(new TestClass(TestClassWithNamedDataPoints.class)); List assignments = supplier.getValueSources(signature("methodWantingWrongNamedString")); diff --git a/src/test/java/org/junit/tests/experimental/theories/runner/FailingDataPointMethods.java b/src/test/java/org/junit/tests/experimental/theories/runner/FailingDataPointMethods.java new file mode 100644 index 000000000000..5abc0ea480fb --- /dev/null +++ b/src/test/java/org/junit/tests/experimental/theories/runner/FailingDataPointMethods.java @@ -0,0 +1,136 @@ +package org.junit.tests.experimental.theories.runner; + +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertThat; +import static org.junit.experimental.results.PrintableResult.testResult; +import static org.junit.experimental.results.ResultMatchers.isSuccessful; +import org.junit.Test; +import org.junit.experimental.theories.DataPoint; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; + +public class FailingDataPointMethods { + + @RunWith(Theories.class) + public static class HasFailingSingleDataPointMethod { + @DataPoint + public static int num = 10; + + @DataPoint + public static int failingDataPoint() { + throw new RuntimeException(); + } + + @Theory + public void theory(int x) { + } + } + + @Test + public void shouldFailFromExceptionsInSingleDataPointMethods() { + assertThat(testResult(HasWronglyIgnoredFailingSingleDataPointMethod.class), not(isSuccessful())); + } + + @RunWith(Theories.class) + public static class HasFailingDataPointArrayMethod { + @DataPoints + public static int[] num = { 1, 2, 3 }; + + @DataPoints + public static int[] failingDataPoints() { + throw new RuntimeException(); + } + + @Theory + public void theory(int x) { + } + } + + @Test + public void shouldFailFromExceptionsInDataPointArrayMethods() { + assertThat(testResult(HasFailingDataPointArrayMethod.class), not(isSuccessful())); + } + + @RunWith(Theories.class) + public static class HasIgnoredFailingSingleDataPointMethod { + @DataPoint + public static int num = 10; + + @DataPoint(ignoredExceptions=Throwable.class) + public static int failingDataPoint() { + throw new RuntimeException(); + } + + @Theory + public void theory(int x) { + } + } + + @Test + public void shouldIgnoreSingleDataPointMethodExceptionsOnRequest() { + assertThat(testResult(HasIgnoredFailingSingleDataPointMethod.class), isSuccessful()); + } + + @RunWith(Theories.class) + public static class HasIgnoredFailingMultipleDataPointMethod { + @DataPoint + public static int num = 10; + + @DataPoints(ignoredExceptions=Throwable.class) + public static int[] failingDataPoint() { + throw new RuntimeException(); + } + + @Theory + public void theory(int x) { + } + } + + @Test + public void shouldIgnoreMultipleDataPointMethodExceptionsOnRequest() { + assertThat(testResult(HasIgnoredFailingMultipleDataPointMethod.class), isSuccessful()); + } + + @RunWith(Theories.class) + public static class HasWronglyIgnoredFailingSingleDataPointMethod { + @DataPoint + public static int num = 10; + + @DataPoint(ignoredExceptions=NullPointerException.class) + public static int failingDataPoint() { + throw new RuntimeException(); + } + + @Theory + public void theory(int x) { + } + } + + @Test + public void shouldNotIgnoreNonMatchingSingleDataPointExceptions() { + assertThat(testResult(HasWronglyIgnoredFailingSingleDataPointMethod.class), not(isSuccessful())); + } + + @RunWith(Theories.class) + public static class HasWronglyIgnoredFailingMultipleDataPointMethod { + @DataPoint + public static int num = 10; + + @DataPoint(ignoredExceptions=NullPointerException.class) + public static int failingDataPoint() { + throw new RuntimeException(); + } + + @Theory + public void theory(int x) { + } + } + + @Test + public void shouldNotIgnoreNonMatchingMultipleDataPointExceptions() { + assertThat(testResult(HasWronglyIgnoredFailingMultipleDataPointMethod.class), not(isSuccessful())); + } + +} diff --git a/src/test/java/org/junit/tests/experimental/theories/runner/WithAutoGeneratedDataPoints.java b/src/test/java/org/junit/tests/experimental/theories/runner/WithAutoGeneratedDataPoints.java index 6e0fbae6d14a..26eb5156e43b 100644 --- a/src/test/java/org/junit/tests/experimental/theories/runner/WithAutoGeneratedDataPoints.java +++ b/src/test/java/org/junit/tests/experimental/theories/runner/WithAutoGeneratedDataPoints.java @@ -23,13 +23,13 @@ public void theory(boolean b) { } @Test - public void shouldAutomaticallyGenerateEnumDataPoints() throws Exception { + public void shouldAutomaticallyGenerateEnumDataPoints() throws Throwable { assertEquals(ENUM.values().length, potentialAssignments( TheoryTestClassWithAutogeneratedParameterValues.class.getMethod("theory", ENUM.class)).size()); } @Test - public void shouldAutomaticallyGenerateBooleanDataPoints() throws Exception { + public void shouldAutomaticallyGenerateBooleanDataPoints() throws Throwable { assertEquals(2, potentialAssignments( TheoryTestClassWithAutogeneratedParameterValues.class.getMethod("theory", boolean.class)).size()); } @@ -46,7 +46,7 @@ public void theory(ENUM e) { } @Test - public void shouldNotAutogenerateEnumDataPointsWhenSpecificDataPointGiven() throws Exception { + public void shouldNotAutogenerateEnumDataPointsWhenSpecificDataPointGiven() throws Throwable { assertEquals(1, potentialAssignments( TheoryTestClassWithSpecificEnumDataPoint.class.getMethod("theory", ENUM.class)).size()); } @@ -63,7 +63,7 @@ public void theory(boolean b) { } @Test - public void shouldNotAutogenerateBooleanDataPointsWhenSpecificDataPointGiven() throws Exception { + public void shouldNotAutogenerateBooleanDataPointsWhenSpecificDataPointGiven() throws Throwable { assertEquals(1, potentialAssignments( TheoryTestClassWithSpecificBooleanDataPoint.class.getMethod("theory", boolean.class)).size()); } diff --git a/src/test/java/org/junit/tests/experimental/theories/runner/WithDataPointMethod.java b/src/test/java/org/junit/tests/experimental/theories/runner/WithDataPointMethod.java index 581cac89037c..c814508de897 100644 --- a/src/test/java/org/junit/tests/experimental/theories/runner/WithDataPointMethod.java +++ b/src/test/java/org/junit/tests/experimental/theories/runner/WithDataPointMethod.java @@ -33,25 +33,6 @@ public static int oneHundred() { @Theory public void allIntsOk(int x) { - - } - } - - @RunWith(Theories.class) - public static class HasUglyDataPointMethod { - @DataPoint - public static int oneHundred() { - return 100; - } - - @DataPoint - public static int oneUglyHundred() { - throw new RuntimeException(); - } - - @Theory - public void allIntsOk(int x) { - } } @@ -60,11 +41,6 @@ public void pickUpDataPointMethods() { assertThat(testResult(HasDataPointMethod.class), isSuccessful()); } - @Test - public void ignoreExceptionsFromDataPointMethods() { - assertThat(testResult(HasUglyDataPointMethod.class), isSuccessful()); - } - @RunWith(Theories.class) public static class DataPointMethodReturnsMutableObject { @DataPoint @@ -93,32 +69,29 @@ public void mutableObjectsAreCreatedAfresh() { @RunWith(Theories.class) public static class HasDateMethod { @DataPoint - public int oneHundred() { + public static int oneHundred() { return 100; } - public Date notADataPoint() { + public static Date notADataPoint() { return new Date(); } @Theory public void allIntsOk(int x) { - } @Theory public void onlyStringsOk(String s) { - } @Theory public void onlyDatesOk(Date d) { - } } @Test - public void ignoreDataPointMethodsWithWrongTypes() throws Exception { + public void ignoreDataPointMethodsWithWrongTypes() throws Throwable { assertThat(potentialAssignments( HasDateMethod.class.getMethod("onlyStringsOk", String.class)) .toString(), not(containsString("100"))); diff --git a/src/test/java/org/junit/tests/experimental/theories/runner/WithExtendedParameterSources.java b/src/test/java/org/junit/tests/experimental/theories/runner/WithExtendedParameterSources.java index e44d91a632f5..bcda189588bf 100644 --- a/src/test/java/org/junit/tests/experimental/theories/runner/WithExtendedParameterSources.java +++ b/src/test/java/org/junit/tests/experimental/theories/runner/WithExtendedParameterSources.java @@ -1,6 +1,7 @@ package org.junit.tests.experimental.theories.runner; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; import static org.junit.experimental.results.PrintableResult.testResult; @@ -33,12 +34,13 @@ public void testedOnLimitsParameters() throws Exception { } @RunWith(Theories.class) - public static class ShouldFilterNull { - @DataPoint - public static String NULL = null; + public static class ShouldFilterOutNullSingleDataPoints { @DataPoint public static String A = "a"; + + @DataPoint + public static String NULL = null; @Theory(nullsAccepted = false) public void allStringsAreNonNull(String s) { @@ -47,10 +49,41 @@ public void allStringsAreNonNull(String s) { } @Test - public void shouldFilterNull() { - assertThat(testResult(ShouldFilterNull.class), isSuccessful()); + public void shouldFilterOutNullSingleDataPoints() { + assertThat(testResult(ShouldFilterOutNullSingleDataPoints.class), isSuccessful()); + } + + @RunWith(Theories.class) + public static class ShouldFilterOutNullElementsFromDataPointArrays { + @DataPoints + public static String[] SOME_NULLS = { "non-null", null }; + + @Theory(nullsAccepted = false) + public void allStringsAreNonNull(String s) { + assertThat(s, notNullValue()); + } } + @Test + public void shouldFilterOutNullElementsFromDataPointArrays() { + assertThat(testResult(ShouldFilterOutNullElementsFromDataPointArrays.class), isSuccessful()); + } + + @RunWith(Theories.class) + public static class ShouldRejectTheoriesWithOnlyDisallowedNullData { + @DataPoints + public static String value = null; + + @Theory(nullsAccepted = false) + public void allStringsAreNonNull(String s) { + } + } + + @Test + public void ShouldRejectTheoriesWithOnlyDisallowedNullData() { + assertThat(testResult(ShouldRejectTheoriesWithOnlyDisallowedNullData.class), not(isSuccessful())); + } + @RunWith(Theories.class) public static class DataPointArrays { public static String log = ""; diff --git a/src/test/java/org/junit/tests/experimental/theories/runner/WithNamedDataPoints.java b/src/test/java/org/junit/tests/experimental/theories/runner/WithNamedDataPoints.java index 4b88eb7b7072..249ef1041c4b 100644 --- a/src/test/java/org/junit/tests/experimental/theories/runner/WithNamedDataPoints.java +++ b/src/test/java/org/junit/tests/experimental/theories/runner/WithNamedDataPoints.java @@ -60,7 +60,7 @@ public void theory(@FromDataPoints("named") String param) { } @Test - public void onlyUseSpecificDataPointsIfSpecified() throws Exception { + public void onlyUseSpecificDataPointsIfSpecified() throws Throwable { List assignments = potentialAssignments(HasSpecificDatapointsParameters.class .getMethod("theory", String.class)); diff --git a/src/test/java/org/junit/tests/experimental/theories/runner/WithParameterSupplier.java b/src/test/java/org/junit/tests/experimental/theories/runner/WithParameterSupplier.java index 53760278dc0f..1802390a87d4 100644 --- a/src/test/java/org/junit/tests/experimental/theories/runner/WithParameterSupplier.java +++ b/src/test/java/org/junit/tests/experimental/theories/runner/WithParameterSupplier.java @@ -73,7 +73,7 @@ public void theoryMethod(@ParametersSuppliedBy(SimpleSupplier.class) String para } @Test - public void shouldPickUpDataPointsFromParameterSupplier() throws Exception { + public void shouldPickUpDataPointsFromParameterSupplier() throws Throwable { List assignments = potentialAssignments(TestClassUsingParameterSupplier.class .getMethod("theoryMethod", String.class));