4 Replies Latest reply on Jan 5, 2014 2:00 PM by rhanus

    Observing in Arquillian, event not propagated to injected bean

    young_matthewd

      Using Arquillian JUnit (version 1.1.1 respectively 4.11) to setup my CDI (1.0.0.CR7 on Weld 1.1.13.Final) embedded with JDK 1.7.25....

       

      Got the following classes:

      public class Receiver {

      private String message;

       

      public void catching(@Observes DummyEvent event) {

      System.out.println(String.format("message [%s], hash [%d]", event.getMessage(), hashCode()));

      this.message = event.getMessage();

      }

       

      public String getMessage() {

      return this.message;

      }

      }

       

      public class Sender {

      @Inject @Any

      Event<DummyEvent> e;

       

      public void fire(String message) {

      System.out.println(String.format("fire message [%s], hash [%d]", message, hashCode()));

       

      DummyEvent de = new DummyEvent;

      de.setMessage(message);

      e.fire(de);

      }

      }

       

      @RunWith(Arquillian.class)

      public class Example {

      @Deployment

      public static JavaArchive createDeployment() {

      return ShrinkWrap.create(JavaArchive.class).addClasses(Example .class, Receiver.class, Sender.class)

               .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

      }

       

      @Inject

      Receiver receiver;

       

      @Inject

      Sender sender;

       

      @Test

      public void dummy() {

      System.out.println(String.format("sender hash [%d]", sender.hashCode()));

      sender.fire("Hello from me");

       

      System.out.println(String.format("message [%s], receiver hash: [%d]",

      receiver.getMessage(), receiver.hashCode()));

      }

      }

       

      What gets printed is the following:

      sender hash [785714873]

      firing event [value: Hello from me], hash [785714873]

      message [Hello from me], hash [632925108]

      message [null], receiver hash: [1085786565]

       

      What I don't get is why the receiver hash codes differ and the event is NOT propagated to the injected Receiver bean in the Example JUnit.  Rather the event is sent to some OTHER receiver bean.

       

      Has this something to do with scope (which should be the default @Dependent)?

       

      Tested with weld-se and a JUnit wrapper WeldJUnit4Runner and got same results.  Not an Arquillian issue.

        • 1. Re: Observing in Arquillian, event not propagated to injected bean
          young_matthewd

          If I put a conditional observes on the Receiver and set it as ApplicationScoped then the event is never propaged:

          public void catching(@Observes(notifyObserver=Reception.IF_EXISTS) DummyEvent event) {.....

           

          With non-conditional (Reception.ALWAYS) it looks like a Receiver bean is created for every event fired.  Not what I expected nor do I understand why the injected receiver bean never has it's observing method called?

           

          Putting Singleton on the Receiver gives the correct behavior except I would love not to be restricted to a singletone.

          • 2. Re: Observing in Arquillian, event not propagated to injected bean
            rhanus

            What I don't get is why the receiver hash codes differ and the event is NOT propagated to the injected Receiver bean in the Example JUnit.  Rather the event is sent to some OTHER receiver bean.

             

            Has this something to do with scope (which should be the default @Dependent)?

            definitely both receiver and sender beans are of dependent scope so that observer method is called on a different bean

            • 3. Re: Observing in Arquillian, event not propagated to injected bean
              rhanus

              If I put a conditional observes on the Receiver and set it as ApplicationScoped then the event is never propaged:

              public void catching(@Observes(notifyObserver=Reception.IF_EXISTS) DummyEvent event) {.....

              that's correct because receiver bean is not called before the delivery of a event

              if you called receiver bean before firing the event /* receiver.getMessage() */ then it would work fine

              With non-conditional (Reception.ALWAYS) it looks like a Receiver bean is created for every event fired.  Not what I expected nor do I understand why the injected receiver bean never has it's observing method called?

              if receiver bean is of application scope then this scenario should wotk

              • 4. Re: Observing in Arquillian, event not propagated to injected bean
                rhanus

                see a very similar working sample here