I have some methods advised by two aspects, one is using the spring AOP support and the other is a `BeanPostProcessor` (`MethodValidationPostProcessor` specifically) which advises all method with `@Validation` annotation. With my unit tests I am trying to force throwing an error by breaking the method contract, but sometimes the validation is in place (the advise imposed by the above mentioned post processor) and sometimes does not work. Does anybody have experienced something similar.
Here is a small snippet of what I am trying to do (Aspect code):
Annotation (Loggable Code):Code:@Aspect @Component public final class LoggingAspect { @Before(value = "execution(public * * (..)) && @annotation(loggable)", argNames = "joinPoint, loggable") public void before(JoinPoint joinPoint, Loggable loggable) { //logging here... } ...}
Interface being annotated with @Validated annotation (Here's a link with related info)Code:@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Loggable {...}
Code:@Validated public interface Dao<T, K> { T findById(@NotNull K id); T persist(@NotNull T object); }
A base class implementing this interface:
And a subclass with a particular behavior:Code:public abstract class BaseDao<T, K> implements DAO<T,K> { @Loggable public T persist(T object){...} }
Code:public final class UserDao extends BaseDao<User,Long> { @Loggable public T findById(User object){...} }
And the spring context at last something like this
I am testing by calling both methods with null as the argument, but in some occasions I received an IllegalArgumentException: attempt to create saveOrUpdate event with null entity instead of the MethodConstraintViolationException supposedly raised by the MethodValidationInterceptor which advises/intercepts all public method for @Validated annotated interfaces. I am using spring 3.1, Hibernate Validator 4.2 (as required by spring) and aspectj 1.7. Thanks for your help.Code:<aop:aspectj-autoproxy/> <context:annotation-config/> <context:component-scan base-package="com.my.package"/> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/> <bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/>


Reply With Quote