1 Reply Latest reply on Jul 10, 2013 6:38 AM by Steam s

    JUnit 4 test case with Arquillian for JBossESB action class

    Steam s Newbie

      Hi Guys,

                    I have a jbossesb project which has a few action classes. I need to prepare as a starting point some very basic junit test case for it. I am using pom to build my project.

       

      I have written some standard java test cases (for a very standard j2se class) using junit 4 via junit eclipse plugin.

       

      But I have no idea how to write junit test case for my esb action class? And then how to execute the test case and see the output of the test results in somewhere. I have read somewhere that i need arquillian (for esb dependencies) and sonar frameworks for seeing the output of junit tests, but I have no idea how all this work together and what i need to put in my project pom file as dependencies?

       

      Here is one example of my esb action class.

       

       

      public class checkLocationDetailResponse implements ActionLifecycle {
       
       
                private static Log logger = LogFactory.getLog(checkLocationDetailResponse.class);
       
       
                  public checkLocationDetailResponse(ConfigTree config)
                  {
                  }
                  
                  public Message process(Message message)
                  {
                            String response = (String)message.getBody().get();
                      
                      if (response != null && response.indexOf("Station") > 0 && response.indexOf("BookingId") <= 0)
                      {
                            
                        String [][] params = (String [][])message.getProperties().getProperty("xsl-params");
                        
                        if (params == null)
                        {
                          message.getProperties().setProperty("xsl-params", new String [] []{{"locationDetailMsgResponseNode", response}});
                        }
                        else
                        {
                          String [][] paramsNew = new String [params.length + 1] [2];
                          for (int i = 0; i < params.length; i ++ )
                          {
                            paramsNew [i] [0] = params [i] [0];
                            paramsNew [i] [1] = params [i] [1];
                          }
                          paramsNew [params.length] [0] = "locationDetailMsgResponseNode";
                          paramsNew [params.length] [1] = response;
                          message.getProperties().setProperty("xsl-params", paramsNew);
                        }
                        
                                
                        message.getBody().add(response);
                        
                      }
                    
                    return message;
       
       
                        
                  }
       
       
        
                  public void destroy() throws ActionLifecycleException
                  {
                    // TODO Auto-generated method stub
                  }
       
       
                  public void initialise() throws ActionLifecycleException
                  {
                    // TODO Auto-generated method stub
                  }
                }
      

       

       

      I have implemented this following very basic junit test.

       

       

      package com.traveltainment.integra.middleware.cardelmar.check;
      
      
      import static org.junit.Assert.assertEquals;
      import static org.junit.Assert.assertNotNull;
      import static org.junit.Assert.assertNull;
      import static org.junit.Assert.assertTrue;
      
      
      import org.jboss.arquillian.container.test.api.Deployment;
      import org.jboss.arquillian.junit.Arquillian;
      import org.jboss.shrinkwrap.api.Archive;
      import org.jboss.shrinkwrap.api.ShrinkWrap;
      import org.jboss.shrinkwrap.api.spec.JavaArchive;
      import org.jboss.soa.esb.helpers.ConfigTree;
      import org.jboss.soa.esb.message.Message;
      import org.jboss.soa.esb.message.format.MessageFactory;
      import org.junit.After;
      import org.junit.AfterClass;
      import org.junit.Before;
      import org.junit.BeforeClass;
      import org.junit.Test;
      import static org.junit.Assert.*;
      import org.junit.runner.RunWith;
      
      
      
      
        
      
      
                @RunWith(Arquillian.class)
                public class checkLocationDetailResponseTest {
      
      
                          public checkLocationDetailResponseTest() {
                          }
      
      
                          @Deployment
                          public static Archive createTestArchive() {
                                    return ShrinkWrap.create(JavaArchive.class, "Test.jar").addClass(checkLocationDetailResponse.class);
                          }
      
      
        
        
                @BeforeClass
                public static void setUpClass() {
                }
      
      
                @AfterClass
                public static void tearDownClass() {
                }
      
      
                @Before
                public void setUp() {
                }
      
      
                @After
                public void tearDown() {
                }
      
      
                /**
                 * Test of process method, of class MessageParamAdder.
                 */
                @Test
                public void testProcess() throws Exception {
                          String xml =
                                              "<property name=\"parameters\">"
                                              + "<parameter msg-property-name=\"testMessageProperty\" value=\"messagePropertyValue\"/>"
                                              + "<parameter xsl-param-name=\"testXslParam\" value=\"xslValue\"/>"
                                              + "<parameter msg-property-name=\"testMessagePropertyNoValue\"/>"
                                              + "<parameter xsl-param-name=\"testXslParamNoValue\"/>"
                                              + "</property>";
                          System.out.println("process");
                          Message message = MessageFactory.getInstance().getMessage();
                          ConfigTree configTree = ConfigTree.fromXml(xml);
                          checkLocationDetailResponse messageParamAdder = new checkLocationDetailResponse(configTree);
                          Message res = messageParamAdder.process(message);
                          assertEquals(res.getProperties().getProperty("testMessageProperty"), "messagePropertyValue");
                          assertNull(res.getProperties().getProperty("testMessagePropertyNoValue"));
        
                          String[][] params = (String[][]) res.getProperties().getProperty("xsl-params");
                          assertNotNull(params);
                          assertTrue(params.length == 1);
                          assertEquals(params[0][0], "testXslParam");
                          assertEquals(params[0][1], "xslValue");
                }
      }
      
      

       

       

      The dependencies I am using in my POM are:

       

       

      <dependency>
        <groupId>org.jboss.arquillian</groupId>
        <artifactId>arquillian-bom</artifactId>
        <version>1.1.0.Final</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      
      
      <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <scope>test</scope>
      </dependency>
      
      
      <dependency>
        <groupId>org.jboss.arquillian.testng</groupId>
        <artifactId>arquillian-testng-container</artifactId>
        <scope>test</scope>
      </dependency>
      
      

       

      BUt it does not seem to work, since I get strange exception at my test calss execution through mvn clena install.

       

      Could someone please help me in identifying the problem? I am sure there is something wrogn in my pom configuration and/or test class.

       

      Any help shall be really appreciated.

       

      Thanks.

        • 1. Re: JUnit 4 test case with Arquillian for JBossESB action class
          Steam s Newbie

          Finally managed to get that far with the content of my pom file:

           


          <dependencyManagement>


          <dependencies>



          <dependency>




          <groupId>org.jboss.arquillian</groupId>




          <artifactId>arquillian-bom</artifactId>




          <version>1.0.0.Final</version>




          <scope>import</scope>




          <type>pom</type>



          </dependency>


          </dependencies>

          </dependencyManagement>

          <dependencies>


          <dependency>



          <groupId>org.jboss.spec</groupId>



          <artifactId>jboss-javaee-6.0</artifactId>



          <version>1.0.0.Final</version>



          <type>pom</type>



          <scope>provided</scope>


          </dependency>


          <dependency>



          <groupId>junit</groupId>



          <artifactId>junit</artifactId>



          <version>4.8.1</version>



          <scope>test</scope>


          </dependency>


          <dependency>



          <groupId>org.jboss.arquillian.junit</groupId>



          <artifactId>arquillian-junit-container</artifactId>



          <scope>test</scope>


          </dependency>

          </dependencies>

           

          But now getting this error on mvn clena install:

           

            -------------------------------------------------------

          T E S T S

          -------------------------------------------------------

          Running com.traveltainment.integra.middleware.cardelmar.check.checkLocationDetailResponseTest

          Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.593 sec <<< FAILURE!

           

           

          Results :

           

           

          Tests in error:

            com.traveltainment.integra.middleware.cardelmar.check.checkLocationDetailResponseTest: Could not create a new instance of class org.jboss.arquillian

          .test.impl.EventTestRunnerAdaptor see cause.

           

           

          Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

           

          --

           

          I have looked at the forum for the solution to this problem but to no avail.

           

          Can anyone suggest soem pointers?