Skip to content

Commit 99f4378

Browse files
committed
Update ParentRunner(TestClass) to check for a null parameter.
1 parent fef0ee3 commit 99f4378

File tree

5 files changed

+93
-7
lines changed

5 files changed

+93
-7
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.junit.internal;
2+
3+
/** @since 4.13 */
4+
public final class Checks {
5+
6+
private Checks() {}
7+
8+
/**
9+
* Checks that the given value is not {@code null}.
10+
*
11+
* @param value object reference to check
12+
* @return the passed-in value, if not {@code null}
13+
* @throws NullPointerException if {@code value} is {@code null}
14+
*/
15+
public static <T> T notNull(T value) {
16+
if (value == null) {
17+
throw new NullPointerException();
18+
}
19+
return value;
20+
}
21+
22+
/**
23+
* Checks that the given value is not {@code null}, using the given message
24+
* as the exception message if an exception is thrown.
25+
*
26+
* @param value object reference to check
27+
* @param message message to use if {@code value} is {@code null}
28+
* @return the passed-in value, if not {@code null}
29+
* @throws NullPointerException if {@code value} is {@code null}
30+
*/
31+
public static <T> T notNull(T value, String message) {
32+
if (value == null) {
33+
throw new NullPointerException(String.valueOf(message));
34+
}
35+
return value;
36+
}
37+
}

src/main/java/org/junit/runners/ParentRunner.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.junit.runners;
22

3+
import static org.junit.internal.Checks.notNull;
34
import static org.junit.internal.runners.rules.RuleMemberValidator.CLASS_RULE_METHOD_VALIDATOR;
45
import static org.junit.internal.runners.rules.RuleMemberValidator.CLASS_RULE_VALIDATOR;
56

@@ -92,7 +93,7 @@ protected ParentRunner(Class<?> testClass) throws InitializationError {
9293
* @since 4.13
9394
*/
9495
protected ParentRunner(TestClass testClass) throws InitializationError {
95-
this.testClass = testClass;
96+
this.testClass = notNull(testClass);
9697
validate();
9798
}
9899

src/main/java/org/junit/runners/parameterized/TestWithParameters.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.junit.runners.parameterized;
22

33
import static java.util.Collections.unmodifiableList;
4+
import static org.junit.internal.Checks.notNull;
45

56
import java.util.ArrayList;
67
import java.util.List;
@@ -73,10 +74,4 @@ public String toString() {
7374
return testClass.getName() + " '" + name + "' with parameters "
7475
+ parameters;
7576
}
76-
77-
private static void notNull(Object value, String message) {
78-
if (value == null) {
79-
throw new NullPointerException(message);
80-
}
81-
}
8277
}

src/test/java/org/junit/internal/AllInternalTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
@RunWith(Suite.class)
1414
@SuiteClasses({
1515
AnnotatedBuilderTest.class,
16+
ChecksTest.class,
1617
ErrorReportingRunnerTest.class,
1718
ExpectExceptionTest.class,
1819
FailOnTimeoutTest.class,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.junit.internal;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
5+
import static org.junit.Assert.assertSame;
6+
import static org.junit.Assert.fail;
7+
import static org.junit.internal.Checks.notNull;
8+
import org.junit.Test;
9+
10+
/** Tests for {@link Checks}. */
11+
public class ChecksTest {
12+
13+
@Test
14+
public void notNullShouldReturnNonNullValues() {
15+
Double value = Double.valueOf(3.14);
16+
17+
Double result = notNull(value);
18+
19+
assertSame(value, result);
20+
}
21+
22+
@Test
23+
public void notNullShouldThrowOnNullValues() {
24+
try {
25+
notNull(null);
26+
} catch (NullPointerException e) {
27+
assertNull("message should be null", e.getMessage());
28+
return;
29+
}
30+
fail("NullPointerException expected");
31+
}
32+
33+
@Test
34+
public void notNullWithMessageShouldReturnNonNullValues() {
35+
Float value = Float.valueOf(3.14f);
36+
37+
Float result = notNull(value, "woops");
38+
39+
assertSame(value, result);
40+
}
41+
42+
@Test
43+
public void notNullWithMessageShouldThrowOnNullValues() {
44+
try {
45+
notNull(null, "woops");
46+
} catch (NullPointerException e) {
47+
assertEquals("message does not match", "woops", e.getMessage());
48+
return;
49+
}
50+
fail("NullPointerException expected");
51+
}
52+
}

0 commit comments

Comments
 (0)