1 Reply Latest reply on Jul 6, 2013 5:30 PM by Marcio Dantas

    Mocking interceptor collaborators

    Marcio Dantas Newbie

      Hi to all,


      I'm writing an CDI extension to Easymock.


      Here's an example of how testing looks like:



      public class HelloWorldBusinessTest {




          private HelloWorldBusiness helloWorldBusiness;




          private HelloWorldDao helloWorldDaoMock;



          public void afterTest() {





          public void testSay() {











      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 Newbie

          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;






               * Wraps interceptor injection target.

               * @param interceptor interceptor


              private void wrapInterceptorInjectionTarget(

                  final Interceptor<Object> interceptor) {

                  try {


                      final InjectionTarget<Object> injectionTarget =



                              new TestSubjectInjectionTarget<Object>(injectionTarget));

                  } catch (final RuntimeException e) {

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

                          + ": Couldn't wrap interceptor "

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