Search code examples
junitmockingjava-ee-6cdiinterceptor

mock cdi interceptors during junit tests


I have a complex Java EE 6 app with a web module, an EJB module and some utility jars.

I want to do some integration tests with Junit. Therefore I use the openwebbeans cdi container (Thanks to Mr. Struberg http://struberg.wordpress.com/2012/03/17/controlling-cdi-containers-in-se-and-ee/) It works perfectly. I can start a complete cdi container in a Junit test.

My problem is that I have some interceptors in my application which cannot run in a Junit test (MQ-, persistence- and transaction-interceptors). So I want to mock these interceptor implementations.

Does anybody know how to do this?


Solution

  • To whom it may concern ;-)

    At the end I solved my issue with clean Java EE techniques. I provided a method which observes the ProcessAnnotatedType event. This method evaluates the type which is processed and if it is one of my interceptors, then I veto the processing.

    public void processAnnotatedType(@Observes final ProcessAnnotatedType<?> event, final BeanManager manager) {
        if (event.getAnnotatedType().getJavaClass().equals(PrivilegeCheckingInterceptor.class)) {
            event.veto();
        }       
    }