Friday, March 8, 2013

Your first integration test experience

Illustration here is a testing scenario in which we can upload JAXWS web app to the server , deploy and invoke its services for testing purposes. These are the steps you need to follow.
( Note : you can start writing your tests in the following location according to your as server version :
eg: /home/xxxx/svn/carbon/platform/branches/x.x.x/products/as/x.x.x/modules/integration/tests-new)

1. Create a package and add a new test class. (eg: jaxwssampleservice /

2. Extend class. ( contains some useful utility methods which we can use regularly inside our test classes.)

3. Create a method called init() in class under the annotation @BeforeClass . Now invoke init() method of inside the init() method we just created.
@BeforeClass(alwaysRun = true)
public void init() throws Exception {

(This enables us to get the backendurl of the server and a session cookie as described in step 5.)

4. We can use @Test annotation to add test cases to our test class. Create a method
(eg : webApplicationUpload ) inside our test class.

5 .In-order to upload our artifact to the app server we can invoke the relevant service client from the test automation framework. Since we are testing JAXWS service we can invoke JAXWSWebappAdminClient to upload the artifact "java_first_jaxws.war" to the server. (Note that we need to pass the backendurl and the session cookie when creating an instance from JAXWSWebappAdminClient. We can derive values for these arguments by invoking asServer environment variable of inside JAXWSWebappAdminClient constructor.)
eg :
JAXWSWebappAdminClient   jaxwsWebappAdminClient =
                            new JAXWSWebappAdminClient(asServer.getBackEndUrl(), asServer.getSessionCookie());

We can use uploadWebapp method of class to upload the artifact. (Note that we need to provide the location where "java_first_jaxws.war" resides in our application)
We can add a package (artifacts/AS/jaxws) under resources directory and place our java_first_jaxws.war inside it. Therefore our artifact location would now become :

String location = ProductConstant.SYSTEM_TEST_RESOURCE_LOCATION +"artifacts" + File.separator + "AS" + File.separator + "jaxws" + File.separator +"java_first_jaxws.war";

Now we need to pass this location and the name of the artifact to the uploadwebapp method. We can do this using the instance we created from
jaxwsWebappAdminClient.uploadWebapp(location, "java_first_jaxws.war");

6. To check whether the deployment of the service was successful we can call waitForServiceDeployment method of class.
(Note that we need to provide the service url of the service we deployed. If java_first_jaxws.war contains a service “hello_world” we can verify the deployment as follows)
AxisServiceClientUtils.waitForServiceDeployment(asServer.getWebAppURL() +

7. For this illustration we will call “SayHiToUser” operation inside “hello_world“ service. To invoke the service create a method (serviceRequest) under the annotation @Test and create the request as follows.

String request = "<ns2:sayHiToUser xmlns:ns2=\"http://server.hw.demo/\">" +

Create a new method createPayload .To create the payload pass the request made to it.

private OMElement createPayload(String request) throws XMLStreamException {
return new StAXOMBuilder(new ByteArrayInputStream(request.getBytes())).getDocumentElement();

8. Create an instance of class. In order to send the request we can call sendReceive method of this class. This will return the response from the server.
String endpoint = asServer.getWebAppURL() + "/java_first_jaxws/services/hello_world";
OMElement response = axisServiceClient.sendReceive(createPayload(request), endpoint,

9. We can assert the response using TestNG.
assertEquals(("<ns2:sayHiToUserResponse xmlns:ns2=\"http://server.hw.demo/\">" +
"<return>Hello Galaxy</return></ns2:sayHiToUserResponse>"),

10. After completion of the response validation we can delete / remove the artifact uploaded to the app server by invoking deleteWebAppFile method of class. We can do this under @AfterClass annotation.
@AfterClass(alwaysRun = true)
public void webApplicationDelete() throws Exception {
WebAppAdminClient webAppAdminClient = new WebAppAdminClient(asServer.getBackEndUrl(),
webAppAdminClient.deleteWebAppFile("java_first_jaxws.war");"java_first_jaxws.war deleted successfully");

No comments:

Post a Comment