Skip to content

Commit 4e9f1a6

Browse files
committed
Prevent creation of FrameworkMethod or FrameworkField without underlying member.
The classes are relying on an underlying member. Fail early, because this makes it easier to find mistakes.
1 parent 967438d commit 4e9f1a6

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

src/main/java/org/junit/runners/model/FrameworkField.java

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public class FrameworkField extends FrameworkMember<FrameworkField> {
1616
private final Field fField;
1717

1818
FrameworkField(Field field) {
19+
if (field == null) {
20+
throw new NullPointerException(
21+
"FrameworkField cannot be created without an underlying field.");
22+
}
1923
fField = field;
2024
}
2125

src/main/java/org/junit/runners/model/FrameworkMethod.java

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public class FrameworkMethod extends FrameworkMember<FrameworkMethod> {
2525
* Returns a new {@code FrameworkMethod} for {@code method}
2626
*/
2727
public FrameworkMethod(Method method) {
28+
if (method == null) {
29+
throw new NullPointerException(
30+
"FrameworkMethod cannot be created without an underlying method.");
31+
}
2832
fMethod = method;
2933
}
3034

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.junit.runners.model;
2+
3+
import static org.junit.rules.ExpectedException.none;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.junit.rules.ExpectedException;
7+
8+
public class FrameworkFieldTest {
9+
@Rule
10+
public final ExpectedException thrown = none();
11+
12+
@Test
13+
public void cannotBeCreatedWithoutUnderlyingField() {
14+
thrown.expect(NullPointerException.class);
15+
thrown.expectMessage("FrameworkField cannot be created without an underlying field.");
16+
new FrameworkField(null);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.junit.runners.model;
2+
3+
import static org.junit.rules.ExpectedException.none;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.junit.rules.ExpectedException;
7+
8+
public class FrameworkMethodTest {
9+
@Rule
10+
public final ExpectedException thrown = none();
11+
12+
@Test
13+
public void cannotBeCreatedWithoutUnderlyingField() {
14+
thrown.expect(NullPointerException.class);
15+
thrown.expectMessage("FrameworkMethod cannot be created without an underlying method.");
16+
new FrameworkMethod(null);
17+
}
18+
}

src/test/java/org/junit/tests/AllTests.java

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.junit.runner.notification.SynchronizedRunListenerTest;
1616
import org.junit.runners.Suite;
1717
import org.junit.runners.Suite.SuiteClasses;
18+
import org.junit.runners.model.FrameworkFieldTest;
19+
import org.junit.runners.model.FrameworkMethodTest;
1820
import org.junit.tests.assertion.AssertionTest;
1921
import org.junit.tests.assertion.ComparisonFailureTest;
2022
import org.junit.tests.assertion.MultipleFailureExceptionTest;
@@ -196,6 +198,8 @@
196198
JUnitCommandLineParseResultTest.class,
197199
FilterFactoriesTest.class,
198200
CategoryFilterFactoryTest.class,
201+
FrameworkFieldTest.class,
202+
FrameworkMethodTest.class,
199203
JUnitCoreTest.class
200204
})
201205
public class AllTests {

0 commit comments

Comments
 (0)