3 Replies Latest reply on Jun 2, 2007 8:24 AM by Chris Lowe

    Inject a mocked component for integration testing

    Denis Newbie

      How can I inject a mocked Renderer into my component for integration testing?

      @Scope(ScopeType.CONVERSATION)
      @Name("myService")
      public class MyService {
      
       @In(create = true)
       private Renderer renderer;
      
       public void send() {
       renderer.render("/myEmail.xhtml");
       }
      }
      


      test.....

      Renderer mockRenderer = getMockRenderer();
      MyService myService = (MyService)lookup("myService");
      // inject the mockRenderer, or put it somewhere the BijectionInterceptor will find it
      myService.save();
      


        • 1. Re: Inject a mocked component for integration testing
          Chris Lowe Apprentice

          Add your mock class to your test package and annotate it with the following:

          @Install(precedence=MOCK)

          More info in Seam reference section 26.2.1. "Using mocks in integration tests".

          • 3. Re: Inject a mocked component for integration testing
            Chris Lowe Apprentice

            You're welcome. As a general solution to invoking e-mails from test code, I use this:

            import java.io.IOException;
            
            import javax.faces.context.FacesContext;
            
            import org.jboss.seam.InterceptionType;
            import org.jboss.seam.ScopeType;
            import org.jboss.seam.annotations.Install;
            import org.jboss.seam.annotations.Intercept;
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.annotations.Scope;
            import org.jboss.seam.ui.facelet.FaceletsRenderer;
            
            import com.sun.facelets.Facelet;
            
            @Scope(ScopeType.STATELESS)
            @Intercept(InterceptionType.NEVER)
            @Name("org.jboss.seam.core.renderer")
            @Install(precedence = Install.MOCK, classDependencies = { "com.sun.facelets.Facelet" })
            public class FaceletsRendererMock extends FaceletsRenderer
            {
             @Override
             protected void renderFacelet(final FacesContext facesContext,
             final Facelet facelet)
             throws IOException
             {
             // This mock will be used when sending e-mails.
            
             // JSF cannot render in testng environment so this mock version does nothing.
             // Despite this, allowing the rest of the Renderer to run will still catch
             // blatently malformed documents (unterminated XML tags for example).
             }
            
            }


            Place this somewhere in your test source folder.

            Hope this is useful.

            Ps. Sorry it took me so long to reply - I'd not checked the "notify me" option on this thread.