Issue
I have really weird issue. Heres my method
@SomeAnnotation(value = "test")
public void doSomethink() {
..
}
When I'm using my application everythink works fine (when calling method doSomethink()
in debug it also goes inside annotation), but when I run test like below
@Test
public void testDoSomethink() {
service.doSomethink();
}
My test completly ignores annotation and goes straight into doSomethink
method. Am i missing somethink? I think that piece of code is enough but let me know if you need some more.
SomeAnnotation
package org.springframework.security.access.prepost;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface SomeAnnotation{
String value();
}
By ignore i mean that when running through test it simply bypass annotation like its not even there
Solution
Annotations are not code which gets executed, but it is meta-information which needs an annotation processor to do something with it.
In case of Java EE for example, the annotations are processed by the container (e.g. the app server you use) and result in doing additional things like setting up a transaction or do a mapping between entity and table.
So in your case, it seems that you expect Spring to do something with the annotation (which you experience when debugging your application). When running the test case, there is no framework or container doing it, so you have to either mock or simulate that, or use something like Arquillian to run your test within the needed environment (e.g. a container).
Answered By - Alexander Rühl
Answer Checked By - Clifford M. (JavaFixing Volunteer)