mardi 1 septembre 2015

Number of annotations at JUnit test incorrect

I have created some custom annotations to use for system tests which are run via JUnit.

A test e.g. looks like this:

@TestCaseName("Change History")
public class ChangeHistory extends SystemTestBase
{    
    @Test
    @Risk(1)
    public void test()
    {
...

I am now implementing a Test Runner which shall report the test name, the risk and the somewhere for documentation purposes.

public class MyRunner extends BlockJUnit4ClassRunner
{
    ...
    @Override
    protected void runChild(final FrameworkMethod method, RunNotifier notifier) 
    {
        ...
        System.out.println("Class annotations:");
        Annotation[] classanno = klass.getAnnotations();
        for (Annotation annotation : classanno) {
            System.out.println(annotation.annotationType());
        }

        System.out.println("Method annotations:");
        Annotation[] methanno = method.getAnnotations();
        for (Annotation annotation : methanno) {
            System.out.println(annotation.annotationType());
        }

The output is

Class annotations:
Method annotations:
interface org.junit.Test

So getAnnotations() seems to return annotations of JUnit only and not all annotations. This is not mentioned in the documentation of JUnit:

Returns the annotations on this method

The return type is java.lang.Annotation which made me believe that I can use any annotation. I defined the annotation like follows - I just used it and when there was an error I let Eclipse generate the annotation:

public @interface Risk {
    int value();
}

How do I get all annotations of the test class and test method?

Aucun commentaire:

Enregistrer un commentaire