1 Reply Latest reply on Jul 6, 2013 5:30 PM by marcio.dantas

    Mocking interceptor collaborators

    marcio.dantas

      Hi to all,

       

      I'm writing an CDI extension to Easymock.

       

      Here's an example of how testing looks like:

       

      @RunWith(DemoiselleRunner.class)

      public class HelloWorldBusinessTest {

       

          @Inject

          @TestSubject

          private HelloWorldBusiness helloWorldBusiness;

       

          @Inject

          @Mock

          private HelloWorldDao helloWorldDaoMock;

       

          @After

          public void afterTest() {

              reset(helloWorldDaoMock);

          }

       

          @Test

          public void testSay() {

       

              helloWorldDaoMock.doSomething();

              replay(helloWorldDaoMock);

       

              helloWorldBusiness.say();

       

              verify(helloWorldDaoMock);

          }

      }

       

      I would like to unit tests CDI interceptors using the same testing style (I don't want developers to have to write producers or stereotypes).

       

      Is there a way to extend how an interceptor is injected? Like what can be done to other beans observing ProcessInjectionTarget?

        • 1. Re: Mocking interceptor collaborators
          marcio.dantas

          I was able to wrap the interceptor's injection target using the ProcessBean lifecycle event as below.

          But don't know if it's the best way and still would like some recommendations on this. Cheers.

           

           

          public class EasymockCdiExtension implements Extension {

           

          ...

           

          /**

               * Processes enabled interceptors.

               * @param event enabled bean event

               * @param manager bean mnager

               */

              public void processEnabledInterceptor(

                  @Observes final ProcessBean<Object> event) {

                  final Bean<Object> bean = event.getBean();

                  if (bean instanceof Interceptor) {

                      final Interceptor<Object> interceptor = (Interceptor<Object>) bean;

                      wrapInterceptorInjectionTarget(interceptor);

                  }

              }

           

              /**

               * Wraps interceptor injection target.

               * @param interceptor interceptor

               */

              private void wrapInterceptorInjectionTarget(

                  final Interceptor<Object> interceptor) {

                  try {

                      @SuppressWarnings("unchecked")

                      final InjectionTarget<Object> injectionTarget =

                          Whitebox.getInternalState(interceptor,InjectionTarget.class);

                      Whitebox.setInternalState(interceptor,

                              new TestSubjectInjectionTarget<Object>(injectionTarget));

                  } catch (final RuntimeException e) {

                      System.err.println("[WARN] " + getClass().getName()

                          + ": Couldn't wrap interceptor "

                          + interceptor.getBeanClass() + ".");

                  }

              }

          ...

          }