This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Writing a Test Case for ESB

This page illustrates how to write a simple ESB integration test class. Most of the ESB integration test is based on synapse configuration or synapse artifacts. The following example test class is used to test proxy service deployment and undeployment. The synapse configuration we are going to test is as follows:

 

<?xml version="1.0" encoding="UTF-8"?>   
  <definitions xmlns="http://ws.apache.org/ns/synapse">    
    <proxy name="simpleProxy" transports="https http"    startOnLoad="true" trace="disable"> 
       <target>   
         <inSequence>   
            <send>   
              <endpoint>   <address uri="http://localhost:9000/services/SimpleStockQuoteService" />    </endpoint>   
            </send>   
         </inSequence>   
         <outSequence>     <send />   </outSequence>   
       </target>   
    </proxy>   
  </definitions> 

Before writing the test case, make sure you have an ESB instance up and running as well as another server with an actual backend service running. TAF looks after all the preconditions needed to run the test class successfully. Before running the test class, the framework starts the ESB server, and the axis2 server with the SimpleStockQuoteService, using the testNG listeners. The users defined in the userlist.csv (tenantlist.csv) file located in the Resources directory are also populated by the framework.  

Within the test class, focus only on the test scenario. You can start writing your tests in the following location according to your ESB server version:

platform/branches/x.x.x/products/esb/x.x.x/modules/integration/tests
All the test classes are placed in the modules below and are found under the Integration module:
  1. tests-other
  2. tests-sample
  3. tests-transport
  4. tests-service
  5. tests-mediator

Select the correct module as the test scenario, create a proper packaging and add a java class extending ESBIntegrationTest. Put the synapse configuration file in artifact/ESB under the Resources folder.

package org.wso2.carbon.esb.proxyservice.test.passThroughProxy;  
 import org.apache.axiom.om.OMElement;  
 import org.testng.annotations.AfterClass;  
 import org.testng.annotations.BeforeClass;  
 import org.testng.annotations.Test;  
 import org.wso2.carbon.esb.ESBIntegrationTest;  
 import javax.xml.namespace.QName;  
 import static org.testng.Assert.assertEquals;  
 
public class ProxyServiceDeploymentTestCase extends ESBIntegrationTest {  

   @BeforeClass(alwaysRun = true)  
   public void deployService() throws Exception {  
     // initializing server configuration  
     super.init(2);  
     // deploying the artifact defined in the proxy_service.xml  
     loadESBConfigurationFromClasspath( "/artifacts/ESB/proxyconfig/proxy/passThroughProxy/proxy_service.xml");  
   }  

   @Test(groups = "wso2.esb", description = "Pass through proxy service invocation test")  
   public void testPassThroughProxy() throws Exception {  
     // invoking the proxy service and getting the response using a service client   
     OMElement response = axis2Client.sendSimpleStockQuoteRequest(getProxyServiceURL("StockQuoteProxy"), null, "WSO2");  
     // response assertions  
     String symbol = response.getFirstElement().getFirstChildWithName(new QName("http://services.samples/xsd", "symbol"))   .getText();  
     assertEquals(symbol, "WSO2", "Fault: value 'symbol' mismatched");  
   }  

   @AfterClass(alwaysRun = true)  
   public void unDeployService() throws Exception {  
     // undeploying deployed artifact  
     super.cleanup();  
   }  
 }

ESBIntegrationTest abstracts most of the useful methods for ESB testing and once it is inherited, it will provide the important features.

Init() will build the ESB server configurations such as the backend url, service url, etc. and authenticate the user defined in the userlist.csv with the server. It is called under @BeforeClass() and will run before all the test methods.

loadESBConfigurationFromClasspath (/artifacts/ESB/$path/synapse_config.xml) will deploy the artifact defined in the file (proxy, endpoints, sequence, message store, etc.) using admin services. It goes through the configuration and deploys each artifact one by one. If the same artifact name is already in the system, it is deleted and a new one is deployed.

getProxyServiceURL ("StockQuoteProxy") returns the proxy service url (http).

cleanup() will undeploy the deployed artifact and it is called under @AfterClass(). It will run after all the test methods.

The above flow executes your test scenario clearly and if any exception is thrown, an assertion failure happens, indicating a test failure.

You can write many test cases as shown above. In addition, there are more utility methods provided by Test Automation Framework.

To run the test class, add your test classes in the testng.xml under the Resources directory.

 

<test name="first-test" preserve-order="true" verbose="2">  
     <classes>  
       <class name="org.wso2.carbon.esb.proxyservice.test.passThroughProxy.ProxyServiceDeploymentTestCase"/>  
     </classes>  
   </test>
You can also specify to run all classes by adding the package name.

 

<test name="first-test" preserve-order="true" verbose="2">  
     <packages>  
       <package name="org.wso2.carbon.esb.proxyservice.test.passThroughProxy"/>  
     </packages>  
   </test>
Issue an MVN clean install to execute your test classes and generate the reports in target/surefire-reports.