Skip to content

Commit 0e1a559

Browse files
committed
Override toString() with meaningful implementation.
The new implementation provides more useful information.
1 parent 4e9f1a6 commit 0e1a559

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,9 @@ public Class<?> getDeclaringClass() {
7777
public Object get(Object target) throws IllegalArgumentException, IllegalAccessException {
7878
return fField.get(target);
7979
}
80+
81+
@Override
82+
public String toString() {
83+
return fField.toString();
84+
}
8085
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,9 @@ public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
213213
public List<ParameterSignature> getParameterSignatures() {
214214
return ParameterSignature.signatures(fMethod);
215215
}
216+
217+
@Override
218+
public String toString() {
219+
return fMethod.toString();
220+
}
216221
}

src/test/java/org/junit/runners/model/FrameworkFieldTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package org.junit.runners.model;
22

3+
import static org.junit.Assert.assertTrue;
34
import static org.junit.rules.ExpectedException.none;
5+
6+
import java.lang.reflect.Field;
7+
48
import org.junit.Rule;
59
import org.junit.Test;
610
import org.junit.rules.ExpectedException;
@@ -15,4 +19,16 @@ public void cannotBeCreatedWithoutUnderlyingField() {
1519
thrown.expectMessage("FrameworkField cannot be created without an underlying field.");
1620
new FrameworkField(null);
1721
}
22+
23+
@Test
24+
public void hasToStringWhichPrintsFieldName() throws Exception {
25+
Field field = ClassWithDummyField.class.getField("dummyField");
26+
FrameworkField frameworkField = new FrameworkField(field);
27+
assertTrue(frameworkField.toString().contains("dummyField"));
28+
}
29+
30+
private static class ClassWithDummyField {
31+
@SuppressWarnings("unused")
32+
public final int dummyField = 0;
33+
}
1834
}

src/test/java/org/junit/runners/model/FrameworkMethodTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package org.junit.runners.model;
22

3+
import static org.junit.Assert.assertTrue;
34
import static org.junit.rules.ExpectedException.none;
5+
6+
import java.lang.reflect.Method;
7+
48
import org.junit.Rule;
59
import org.junit.Test;
610
import org.junit.rules.ExpectedException;
@@ -15,4 +19,17 @@ public void cannotBeCreatedWithoutUnderlyingField() {
1519
thrown.expectMessage("FrameworkMethod cannot be created without an underlying method.");
1620
new FrameworkMethod(null);
1721
}
22+
23+
@Test
24+
public void hasToStringWhichPrintsMethodName() throws Exception {
25+
Method method = ClassWithDummyMethod.class.getMethod("dummyMethod");
26+
FrameworkMethod frameworkMethod = new FrameworkMethod(method);
27+
assertTrue(frameworkMethod.toString().contains("dummyMethod"));
28+
}
29+
30+
private static class ClassWithDummyMethod {
31+
@SuppressWarnings("unused")
32+
public void dummyMethod() {
33+
}
34+
}
1835
}

0 commit comments

Comments
 (0)