Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test annotation in java using junit

The class having

@Info(name = "ABC", version = "1.1.1")
public class Test implements TestPlugin{
}

Where @Info is @interface annotation

I want to write junit test case to verify its @info name and version, what i have passed.

like image 735
prateem tadas Avatar asked Sep 14 '26 18:09

prateem tadas


1 Answers

You can use reflection for this:

Annotation annotation = Test.class.getAnnotation(Info.class);

        if(annotation instanceof Info){
            Info info = (Info) annotation;

            assertEquals("ABC", info.name());
            assertEquals("1.1.1", info.version());
        } else {
            fail("Did not find annotation");
        }
like image 153
Albert Bos Avatar answered Sep 17 '26 08:09

Albert Bos