4 Replies Latest reply on Oct 6, 2007 3:51 PM by alesj

    Injecting beans into Unit Tests under 1.02

    jonesn

      Hi All.

      I'm looking to introduce some injection into my current works Test Suite. They run their apps under JBoss so I thought Microcontainer would be the best fit for them.

      I was having a look at MicrocontainerTest and MicrocontainerTestDelegate but I'm not too sure what their envisaged usage is.

      What I'm looking to do is inject beans into integration tests, things like Datasources etc. I'm from a Spring background and in the past I would have extended AbstractDependencyInjectionSpringContextTests which would inject beans from the supplied contexts into the tests.

      Does Microcontainer offer something similar? If not could someone point me in the direction of an example or the like to show me how Injection into Tests is achieved with Microcontainer?

      Thanks for your help.
      Nick.

        • 1. Re: Injecting beans into Unit Tests under 1.02
          alesj

          You can use MicrocontainerTest and MicrocontainerTestDelegate to do your injection.
          They have all the handy methods to provide you the beans you deployed, e.g.:

           /**
           * Get a bean
           *
           * @param name the bean name
           * @return the bean
           * @throws IllegalStateException when the bean does not exist
           */
           protected Object getBean(Object name)
           {
           return getBean(name, ControllerState.INSTALLED);
           }
          
           /**
           * Get a bean
           *
           * @param name the name of the bean
           * @param state the state of the bean
           * @return the bean
           * @throws IllegalStateException when the bean does not exist at that state
           */
           protected Object getBean(Object name, ControllerState state)
           {
           return getMCDelegate().getBean(name, state);
           }
          
           /**
           * Get a context
           *
           * @param name the bean name
           * @return the context
           * @throws IllegalStateException when the context does not exist
           */
           protected KernelControllerContext getControllerContext(Object name)
           {
           return getControllerContext(name, ControllerState.INSTALLED);
           }
          
           /**
           * Get a context
           *
           * @param name the name of the bean
           * @param state the state of the bean
           * @return the context
           * @throws IllegalStateException when the context does not exist at that state
           */
           protected KernelControllerContext getControllerContext(Object name, ControllerState state)
           {
           return getMCDelegate().getControllerContext(name, state);
           }
          


          You simply provide the XML file with the beans, how you actually use that injection is up to you.

          Check out how we use it in our tons of test cases.


          • 2. Re: Injecting beans into Unit Tests under 1.02
            jonesn

            Hi, I've built the 2.0 release and read through some of your Tests. Based on that I've got a few dbunit tests working using the lookup means mentioned.

            Is there a way to have beans configured in the context automatically injected into the Tests that need them?

            For example you have a base class that extends MicrocontainerTest that loads the contexts. Then any class extending that base class which has a setter matching the name of a bean in the context will automatically be wired using the Microcontatiner managed bean.

            Cheers
            Nick.

            • 3. Re: Injecting beans into Unit Tests under 1.02
              alesj

               

              "jonesn" wrote:

              Is there a way to have beans configured in the context automatically injected into the Tests that need them?

              For example you have a base class that extends MicrocontainerTest that loads the contexts. Then any class extending that base class which has a setter matching the name of a bean in the context will automatically be wired using the Microcontatiner managed bean.


              Currently there is no such Test class. We didn't have any need for it. :-)

              But I guess you can easily extend the MicrocontainerTest with the mentioned functionality. Simply register the actual Test instance into underlying controller - and pre-annotate it before ;-), and the contextual injection should be there.

              If you still have problems, let me know, and I'll hack something up. Or if you already have it, please share it. :-)

              • 4. Re: Injecting beans into Unit Tests under 1.02
                alesj

                 

                "alesj" wrote:
                and I'll hack something up


                Added a new useful test class.
                The one you are looking for. ;-)

                Check out WiredMicrocontainerTest + its WiredTestTestCase.