2 Replies Latest reply: Jun 6, 2012 1:16 PM by Bill Murphy RSS

    Arquillian integration with Spring @Autowired (Tomcat 7.0 Managed)

    Bill Murphy Newbie

      Hi,

       

      How do I construct a class under test where one of it's members use Spring @Autowired in a Tomcat 7.0 Managed implementation?

       

      Example code

       

      Class under test: SearchController:

       

      public class SearchController {

       

      @Autowired

      private SearchService searchService;

       

            public List<Integer> searchDocument(String docId, String phrase) {

                    List<Integer>result = searchService.findPageNumbers(docId, phrase);

                    return result;

            }

       

      }

       

      In this case I want an instance of SearchController.  If I write new SearchController() in my Arquillian test class running in separate JVM, I won't trigger the injection of searchSerivce since the Arquillian test class has no idea of Spring or spring context.   I can't use @Inject(CDI) since it not available for Tomcat 7 Managed.

       

      Thanks,

      Bill

        • 1. Re: Arquillian integration with Spring @Autowired (Tomcat 7.0 Managed)
          Jakub Narloch Novice

          Hi Bill,

           

          This is what for the Spring Extension has been made - currently in Alpha 1 ver.: https://github.com/arquillian/arquillian-extension-spring

           

          You need to add the extension into your POM:

           

          <dependency>

               <groupId>org.jboss.arquillian.extension</groupId>
               <artifactId>arquillian-service-deployer-spring-3</artifactId>
               <version>1.0.0.Alpha1</version>
               <scope>test</scope>
          </dependency>

           

          Then you make a standard arquillian test, with one difference - annotate it with @SpringConfiguration with location pointing to yours applicationContext.xml.

          Remeber to include the configuration file with the deployment, otherwise the extension won't be able to instantiate the ApplicationContext:

           

          archive.addAsResource("applicationContext.xml")
          

           

           

          The extension should allow to test JAR and WAR archives.

           

          After setting everything up you should be able to autowire you controller in the test case e.g.:

           

           

          @RunWith(Arquillian.class)
          @SpringConfiguration("applicationContext.xml")
          public class SearchControllerTest {
          
                @Autowired
                private SearchController searchService;
          
                @Test
                public void testSearchDocument {
          
                         // test code
                }
          }
          
          

           

           

          Note that you may have to add additional Spring dependencies into the ShrinkWrap deployment.

          • 2. Re: Arquillian integration with Spring @Autowired (Tomcat 7.0 Managed)
            Bill Murphy Newbie

            thanks for the help Jakub and good luck on your efforts this summer.