2 Replies Latest reply on Mar 2, 2008 8:37 PM by colletts

    AbstractSpringAction Test Case available?

    colletts

      Hi,

      Are there any Junit helper classes for creating test cases for AbstractSpringAction(s)?

      I am looking for something along the lines of spring classes like AbstractDependencyInjectionSpringContextTests.

      Failing that are there any good examples of using Junit to test JBoss ESB actions?

      I tried to create an AbstractSpringAction and ConfigTree programatically in the testcase but I can't seem to get the Spring Container to initialise in a junit test.

      Test Class

      public class MySpringEnabledActionTest extends AbstractDependencyInjectionSpringContextTests{
      
      
       public void testSayHelloAopStyle() throws Exception {
      
       String messageObject = "Message Body String Object";
      
       Message esbMessage = MessageFactory.getInstance().getMessage();
      
       esbMessage.getBody().add(messageObject);
      
       Message returnMessage;
      
       File esbConfigFile = new File("C:/dev/workspace/TestJBossESBSpringAOP/src/main/resources/META-INF/jboss-esb.xml");
      
       FileInputStream fis = new FileInputStream(esbConfigFile);
      
       ConfigTree configTree = ConfigTree.fromInputStream(fis);
      
       MySpringEnabledAction mySpringEnabledAction = new MySpringEnabledAction(configTree);
      
       returnMessage = mySpringEnabledAction.sayHelloAopStyle(esbMessage);
      
       assertSame(messageObject,returnMessage.getBody().get());
      
       }
      
       ...
      }


      this works to a point; but in the actual AbstractSpringAction the spring bean factory is not initialised.

       public class MySpringEnabledAction extends AbstractSpringAction
       {
      
      
       public MySpringEnabledAction(ConfigTree config) throws Exception
       {
       configTree = config;
       }
      
       public Message sayHelloAopStyle(Message message) throws Exception
       {
      
      
       String springContextXml = configTree.getAttribute("springContextXml");
      
       logger.error("springContextXml from contextTree is " + springContextXml); \\ shows that the springContextXml attribute is null
      
      
       BeanFactory beanFactory = getBeanFactory();
      
       if (beanFactory==null){
       logger.error("Beanfactory is null"); \\ because the springContext is null the beanfactory is null??
       } else{
       logger.error("Beanfactory is NOT null");
       }
      
       SaySomething hello = (SaySomething) beanFactory.getBean("helloObject"); \\null pointer exception here
      
       logHeader();
       // interceptor will get applied here
       // check the console output to see the interceptor changed the message
      
       hello.setGreeting((String)message.getBody().get());
      
      
       logFooter();
      
       return message;
       }
      


      The jboss-esb.xml is exactly the same as from the sample quickstart spring_aop i.e. there is a springContextXml attribute pointing to the spring-context.xml

      Using jbossesb-server-4.2.1GA in windows XP win32 with Eclipse 3.3.1.1 and Maven 2.0 running the test cases


      Thanks
      Steve



        • 1. Re: AbstractSpringAction Test Case available?
          marklittle

          If you're looking for Junit tests in general, have a look at the ESB source: it comes with quite a few in there that test all aspects of the ESB as well as the actions. There are some Spring based ones in there too.

          • 2. Re: AbstractSpringAction Test Case available?
            colletts

            Thanks for that tip

            I found a Junit test in

            \jbossesb-4.2.1GA-src\product\services\spring\src\test\java\org\jboss\soa\esb\actions\spring\AbstractSpringActionUnitTest.java


            To fix my original test I needed to explicity set the springContextXml attribute of the configTree after i created it and run the .initialise() method on the instance of the AbstractSpringAction again after i created it in the unit test.

            Modified MySpringEnabledActionTest

            public class MySpringEnabledActionTest extends
            AbstractDependencyInjectionSpringContextTests{
            
             private static Logger logger = Logger.getLogger(MySpringEnabledActionTest.class);
            
             public void testSayHelloAopStyle() throws Exception {
            
             String messageObject = "MySpringEnabledActionTest Message Body String Object";
            
             Message esbMessage = MessageFactory.getInstance().getMessage();
            
             esbMessage.getBody().add(messageObject);
            
             Message returnMessage;
            
             File esbConfigFile = new File("C:/dev/workspace/TestJBossESBSpringAOP/src/main/resources/META-INF/jboss-esb.xml");
            
             logger.error("Config File exists " + esbConfigFile.exists());
            
             FileInputStream fis = new FileInputStream(esbConfigFile);
            
             ConfigTree configTree = ConfigTree.fromInputStream(fis);
            
             logger.error("springContextXml is " + configTree.getAttribute("springContextXml"));
            
             logger.error("Setting springContextXml explictly ");
            
             // Added explicity setting of springContextXml attribute of the ConfigTree
             configTree.setAttribute("springContextXml","/spring-context.xml");
            
             logger.error("springContextXml is " + configTree.getAttribute("springContextXml"));
            
             MySpringEnabledAction mySpringEnabledAction = new MySpringEnabledAction(configTree);
            
             // Added explicit call to initialise the AbstractSpringAction - this loads spring container
             mySpringEnabledAction.initialise();
            
             returnMessage = mySpringEnabledAction.sayHelloAopStyle(esbMessage);
            
             logger.error("ReturnMessage " + returnMessage.getBody().get());
            
             ...
            
             }
            


            Screen output is

            Running org.jboss.soa.esb.samples.quickstart.spring_aop.test.MySpringEnabledActionTest
            11:25:37,017 ERROR [main][MySpringEnabledActionTest] Config File exists true
            11:25:37,033 ERROR [main][MySpringEnabledActionTest] springContextXml is null
            11:25:37,033 ERROR [main][MySpringEnabledActionTest] Setting springContextXml explictly
            11:25:37,033 ERROR [main][MySpringEnabledActionTest] springContextXml is /spring-context.xml
            11:25:37,049 INFO [main][XmlBeanDefinitionReader] Loading XML bean definitions from class path resource [spring-context.xml]
            11:25:37,189 INFO [main][ClassPathXmlApplicationContext] Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=6460907]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [helloObject,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJExpressionPointcut,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor,myInterceptor]; root of BeanFactory hierarchy
            11:25:37,189 INFO [main][ClassPathXmlApplicationContext] 5 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=6460907]
            11:25:37,189 INFO [main][ClassPathXmlApplicationContext] Bean 'org.springframework.aop.config.internalAutoProxyCreator' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
            11:25:37,189 INFO [main][ClassPathXmlApplicationContext] Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@90c06f]
            11:25:37,189 INFO [main][ClassPathXmlApplicationContext] Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@149d886]
            11:25:37,189 INFO [main][DefaultListableBeanFactory] Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [helloObject,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJExpressionPointcut,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor,myInterceptor]; root of BeanFactory hierarchy]
            11:25:37,189 ERROR [main][MySpringEnabledAction] Entering .sayHelloAopStyle(Message)
            11:25:37,189 ERROR [main][MySpringEnabledAction] springContextXml from contextTree is /spring-context.xml
            11:25:37,189 ERROR [main][MySpringEnabledAction] Beanfactory is NOT null