Skip to content

Commit

Permalink
Throw exception with helpful message if annotated field is not public…
Browse files Browse the repository at this point in the history
… (fixes issue junit-team#260).
  • Loading branch information
stefanbirkner committed Jul 9, 2011
1 parent b221845 commit 0c11478
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/main/java/org/junit/runners/model/TestClass.java
@@ -1,5 +1,6 @@
package org.junit.runners.model;

import static java.lang.reflect.Modifier.isPublic;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -138,16 +139,25 @@ public Annotation[] getAnnotations() {
public <T> List<T> getAnnotatedFieldValues(Object test,
Class<? extends Annotation> annotationClass, Class<T> valueClass) {
List<T> results= new ArrayList<T>();
for (FrameworkField each : getAnnotatedFields(annotationClass)) {
for (FrameworkField field : getAnnotatedFields(annotationClass)) {
assertFieldIsPublic(field);
try {
Object fieldValue= each.get(test);
Object fieldValue= field.get(test);
if (valueClass.isInstance(fieldValue))
results.add(valueClass.cast(fieldValue));
} catch (IllegalAccessException e) {
throw new RuntimeException(
"How did getFields return a field we couldn't access?");
"How did getFields return a field we couldn't access?", e);
}
}
return results;
}

public void assertFieldIsPublic(FrameworkField frameworkField) {
Field field= frameworkField.getField();
int modifiers= field.getModifiers();
if (!isPublic(modifiers))
throw new IllegalArgumentException(
"The " + field.getType().getSimpleName() + " '" + field.getName() + "' is not public.");
}
}
21 changes: 21 additions & 0 deletions src/test/java/org/junit/tests/running/classes/TestClassTest.java
@@ -1,5 +1,6 @@
package org.junit.tests.running.classes;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.After;
Expand All @@ -9,10 +10,14 @@
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TestRule;
import org.junit.runners.model.TestClass;

public class TestClassTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();

public static class TwoConstructors {
public TwoConstructors() {
}
Expand Down Expand Up @@ -78,9 +83,25 @@ public static class SubclassWithField extends SuperclassWithField {
public TestRule x;
}

public static class TestWithProtectedTestRule {
@Rule
protected TestRule x;

@Test
public void test() {
}
}

@Test
public void fieldsOnSubclassesShadowSuperclasses() {
assertThat(new TestClass(SubclassWithField.class).getAnnotatedFields(
Rule.class).size(), is(1));
}

@Test
public void shouldThrowExcpetionWithHelpfulMessageForProtectedFields() {
expectedException.expectMessage(equalTo("The TestRule 'x' is not public."));
TestClass testClass = new TestClass(TestWithProtectedTestRule.class);
testClass.getAnnotatedFieldValues(testClass, Rule.class, TestRule.class);
}
}

0 comments on commit 0c11478

Please sign in to comment.