2 Replies Latest reply: Apr 27, 2011 3:46 PM by Todd Morrison RSS

    Weld Event Testing

    Todd Morrison Newbie

      What is the best way to test that Events are firing?

       

      Given qualifier,

       

      @Qualifier @Target({FIELD, PARAMETER}) @Retention(RUNTIME) public @interface Updated {}
      

       

      and Observer registration

       

      public void onAnyDocumentEvent(@Observes Document document) { ... }
      

       

      I have attempted

       

      boolean hasFired = false;
      
      @Inject
      MyUberBean userBean;
      
      @Test
      public void blah()
      {
           Assert.assertFalse(hasFired);
           userBean.documentUpdateMethod();
           Assert.assertTrue(hasFired);
      }
      
      public void documentUpdateHandler(@Observer @Updated Document document)
      {
           hasFired = true;
      } 
      

       

      Although the event is firing in the expected functional flow, it appears that the event callback is actually working on a seperate instance of the Test Container.

       

      This results in the Truth-Assertion to result in false, even though logging shows the event is fired before this assertion.

        • 1. Weld Event Testing
          Aslak Knutsen Master

          That's true.

           

          The TestClass is only non contextual injected for @Inject support, it's not ran in JUnit as a Bean, but it's whithin the BeanArchive so if need be, CDI will create a instance of it(e.g. by the use of @Produces or @Observes), but this is as you state not the same instance as JUnit use.

           

          You need to use statics, or move the Observer out from the TestClass for this to work as expected.

          • 2. Weld Event Testing
            Todd Morrison Newbie

            Static worked just fine.

             

            It should be said that all of my events are firing reliably. I have ~30 so far across 6 modules and I haven't run into any behaviorial bugs.

             

            Great work!