Friday, April 15, 2016

Integration Test using TestNG for Connectors

Hi all, I think you had viewed my last post regarding how to write a connector. Here we are going to see about integration test for connectors which is used to test the connector automatically.
What is TestNG?

TestNG is a testing framework designed to simplify a vast range of testing needs, from unit testing to integration testing. A TestNG test can be configured by @BeforeXXX and @AfterXXX annotations which allows to perform some Java logic before and after a certain point, these points being either of the items listed above. If you want to study more about TestNG framework, there is an official web site you could refer. TestNG supports for Eclipse, IntelliJ IDEA, and NetBeans.

What is Integration Test?

Integration testing is a logical extension of unit testing. In its simplest form, two units that have already been tested are combined into a component and the interface between them is tested. A component, in this sense, refers to an integrated aggregate of more than one unit. In a realistic scenario, many units are combined into components, which are in turn aggregated into even larger parts of the program. The idea is to test combinations of pieces and eventually expand the process to test your modules with those of other groups.

Writing Integration test for Connectors using TestNG
Writing a test is typically a three-step process:
  • Write the business logic of your test and insert TestNG annotations in your code.
  • Add the information about your test (e.g. the class name, the groups you wish to run, etc...) in a testng.xml file or in build.xml.
  • Run TestNG.
 When you clone a repository from the git hub of WSO2, you can view the structure of the project like below. There you can view the test folder. Inside that now we are going to change things according to our need.

 In there we will first see testng.xml and what are the things we have to change according to our need. The structure of the testing.xml is given below.

The things in connectorName has to be changed according to our connector name. After that we have to create JAVA class under the src->test->java->org.wso2.carbon.connector.integration.test->ConnetorName.
In the class we have to import TestNG classes  like below.

import org.testng.Assert;import org.testng.annotations.BeforeClass;import org.testng.annotations.Test;import org.wso2.connector.integration.test.base.ConnectorIntegrationTestBase;

In the java class we have set the environment of the API by giving API url, username, password or OAuth authentication. Setting environment should be done before the test start. So we tell as "@BeforeClass(alwaysRun = true)" before that method.

In the test cases we have to test all available methods which are provided by the API. We have to check the things which are getting through WSO2 ESB is equal to the things getting from direct call to the API. we can import Assert class and check whether
  • both HttpStatusCode are equal.
    Assert.assertEquals(esbRestResponse.getHttpStatusCode(), 200);Assert.assertEquals(apiRestResponse.getHttpStatusCode(), 200);
  • the things come from body messages are equal.
We have to check each test cases giving three type of input such as mandatory, optional and negative.

Post method should be tested by compare the http status code is equal to 201 and get the last inserted value and check whether those values are posted before. Updating methods also have to test like this.

The values for the testing has to be given in the format which can be understand by the API whether it is Soap or JSON.

We have to create request.connectorName folder inside the config folder and write the request. the example of the JSON request is given below.

  "apiURL":"",  "username":"username",  "password":"password"

We have to create proxies.connectorName folder inside the config folder and we have to write the proxy for it.

You can see the already available connectors and build your own connector. 

Happy Coding :)

1 comment: