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



      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 {



      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.




        • 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:





          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:





          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.:



          public class SearchControllerTest {
                private SearchController searchService;
                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.