Skip to content

Commit 236883d

Browse files
committed
Add test and check for null, to stop null datapoints failing in message creation
1 parent 1c9a283 commit 236883d

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public String toString() {
1919

2020
@Override
2121
public String getDescription() throws CouldNotGenerateValueException {
22-
return String.format("%s: %s", name, value.toString());
22+
return String.format("%s: %s", name, (value != null ? value.toString() : "null"));
2323
}
2424
};
2525
}

src/test/java/org/junit/tests/experimental/theories/TheoryFailureMessagesTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@
1515

1616
public class TheoryFailureMessagesTest {
1717

18+
@Test
19+
public void failuresUseNameAndValueWithNullValues() throws InitializationError {
20+
Result result = runTheoryTest(TestWithBadNullValue.class);
21+
22+
Assert.assertEquals(1, result.getFailureCount());
23+
24+
String errorMessage = result.getFailures().get(0).getException().getMessage();
25+
Assert.assertThat(errorMessage, containsString("badDatapoint"));
26+
Assert.assertThat(errorMessage, containsString("null"));
27+
}
28+
1829
@Test
1930
public void failuresUseNameAndValueWithSingleValues() throws InitializationError {
2031
Result result = runTheoryTest(TestWithBadValue.class);
@@ -53,6 +64,21 @@ private static void methodUnderTest(String param) {
5364
throw new IllegalArgumentException("Bad param");
5465
}
5566
}
67+
68+
public static class TestWithBadNullValue {
69+
70+
@DataPoint
71+
public static String oneDatapoint = "good value";
72+
73+
@DataPoint
74+
public static String badDatapoint = null;
75+
76+
@Theory
77+
public void theoryTest(String param) {
78+
methodUnderTest(param);
79+
}
80+
81+
}
5682

5783
public static class TestWithBadValue {
5884

0 commit comments

Comments
 (0)