View Javadoc
1   package org.junit.runners.model;
2   
3   import static org.hamcrest.CoreMatchers.is;
4   import static org.hamcrest.CoreMatchers.nullValue;
5   import static org.junit.Assert.assertThat;
6   import static org.junit.Assert.assertTrue;
7   import static org.junit.rules.ExpectedException.none;
8   
9   import java.lang.annotation.Annotation;
10  import java.lang.reflect.Field;
11  
12  import org.junit.ClassRule;
13  import org.junit.Rule;
14  import org.junit.Test;
15  import org.junit.rules.ExpectedException;
16  
17  public class FrameworkFieldTest {
18      @Rule
19      public final ExpectedException thrown = none();
20  
21      @Test
22      public void cannotBeCreatedWithoutUnderlyingField() {
23          thrown.expect(NullPointerException.class);
24          thrown.expectMessage("FrameworkField cannot be created without an underlying field.");
25          new FrameworkField(null);
26      }
27  
28      @Test
29      public void hasToStringWhichPrintsFieldName() throws Exception {
30          Field field = ClassWithDummyField.class.getField("dummyField");
31          FrameworkField frameworkField = new FrameworkField(field);
32          assertTrue(frameworkField.toString().contains("dummyField"));
33      }
34  
35      @Test
36      public void presentAnnotationIsAvailable() throws Exception {
37          Field field = ClassWithDummyField.class.getField("annotatedField");
38          FrameworkField frameworkField = new FrameworkField(field);
39          Annotation annotation = frameworkField.getAnnotation(Rule.class);
40          assertTrue(Rule.class.isAssignableFrom(annotation.getClass()));
41      }
42  
43      @Test
44      public void missingAnnotationIsNotAvailable() throws Exception {
45          Field field = ClassWithDummyField.class.getField("annotatedField");
46          FrameworkField frameworkField = new FrameworkField(field);
47          Annotation annotation = frameworkField.getAnnotation(ClassRule.class);
48          assertThat(annotation, is(nullValue()));
49      }
50  
51      private static class ClassWithDummyField {
52          @SuppressWarnings("unused")
53          public final int dummyField = 0;
54  
55          @Rule
56          public final int annotatedField = 0;
57      }
58  }