Friday, June 13, 2008

Testing an Annotation

I found neat ways to test annotations.

Here is some annotation test code using JUnit

public class LongParameterListRefactoringTest {

@Test
public void isAnnotationWhenAsked() throws Exception {
assertTrue(LongParameterListRefactoring.class.isAnnotation());
}

@Test
public void annotationIsAnnotated() throws Exception {
assertTrue(LongParameterListRefactoring.class.getAnnotations().length > 0);
}

@Test
public void isAnnotatedWithRetentionAnnotation() throws Exception {
Annotation annotation = LongParameterListRefactoring.class.getAnnotations()[0];
assertTrue(annotation instanceof Retention);
}

@Test
public void isAnnotatedAsSourceCodeAnnotation() throws Exception {
Annotation annotation = LongParameterListRefactoring.class.getAnnotations()[0];
Retention retention = (Retention)annotation;
assertEquals(RetentionPolicy.SOURCE, retention.value());
}

}
This code lived at the SeePeople project.

Note: annotationIsAnnotated has since been changed. Can you guess why?