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.Method;
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 FrameworkMethodTest {
18      @Rule
19      public final ExpectedException thrown = none();
20  
21      @Test
22      public void cannotBeCreatedWithoutUnderlyingField() {
23          thrown.expect(NullPointerException.class);
24          thrown.expectMessage("FrameworkMethod cannot be created without an underlying method.");
25          new FrameworkMethod(null);
26      }
27  
28      @Test
29      public void hasToStringWhichPrintsMethodName() throws Exception {
30          Method method = ClassWithDummyMethod.class.getMethod("dummyMethod");
31          FrameworkMethod frameworkMethod = new FrameworkMethod(method);
32          assertTrue(frameworkMethod.toString().contains("dummyMethod"));
33      }
34  
35      @Test
36      public void presentAnnotationIsAvailable() throws Exception {
37          Method method = ClassWithDummyMethod.class.getMethod("annotatedDummyMethod");
38          FrameworkMethod frameworkMethod = new FrameworkMethod(method);
39          Annotation annotation = frameworkMethod.getAnnotation(Rule.class);
40          assertTrue(Rule.class.isAssignableFrom(annotation.getClass()));
41      }
42  
43      @Test
44      public void missingAnnotationIsNotAvailable() throws Exception {
45          Method method = ClassWithDummyMethod.class.getMethod("annotatedDummyMethod");
46          FrameworkMethod frameworkMethod = new FrameworkMethod(method);
47          Annotation annotation = frameworkMethod.getAnnotation(ClassRule.class);
48          assertThat(annotation, is(nullValue()));
49      }
50  
51      private static class ClassWithDummyMethod {
52          @SuppressWarnings("unused")
53          public void dummyMethod() {
54          }
55  
56          @Rule
57          public void annotatedDummyMethod() {
58          }
59      }
60  }