4 Replies Latest reply on Apr 20, 2014 5:26 AM by dannydan

    CDI Interceptor doesn't get executed

    si.ret

      Hi together,

       

      currently I'd like to test an interceptor which logs occuring exceptions via Arquillian. I've created an interceptor, an InterceptorBinding annotation and annotated my class under test accordingly. But when I'm executing the test the interceptor won't be called. Does Arquillian support CDI interceptors?

       

      My interceptor:

      @Exceptional
      @Interceptor
      public class ExceptionInterceptor {
          @AroundInvoke
          public Object handleException(InvocationContext ctx) throws Exception {
              try {
                  Object o = ctx.proceed();
                  System.out.println("No exception!!");
                  return o;
              } catch (Exception e) {
                  System.out.println("Exception!!");
                  return null;
              }
          }
      }
      

       

      My interceptor binding annotation:

      @InterceptorBinding
      @Target({METHOD, TYPE})
      @Retention(RUNTIME)
      public @interface Exceptional {}
      

       

      My beans-test.xml

      <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
            http://java.sun.com/xml/ns/javaee
            http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
          <interceptors>
              <class>com.xxx.gssn.audit.core.exception.ExceptionInterceptor</class>
          </interceptors>
      </beans>
      

       

      My test class:

      @RunWith(Arquillian.class)
      public class ExceptionHandlingTest {
      
          @Deployment
          public static Archive<?> deploy() {
              return ShrinkWrap
                  .create(JavaArchive.class)
                  .addClasses(ExceptionBean.class, Exceptional.class, ExceptionInterceptor.class)
                  .addAsManifestResource("META-INF/beans-test.xml", "beans.xml");
          }
      
          @Inject
          private ExceptionBean bean;
      
          @Test
          public void testInterceptor() {
              String hello = bean.sayHello();
              assertThat("Hi!", is(hello));
          }
      }
      

       

      Thanks in advance for any comment...