no

Introduction to Web Service Testing With Soapui

I just want to share a simple example on how to create and test a web service on java. Download and install the following: 1.) eclipse ja...

I just want to share a simple example on how to create and test a web service on java.

Download and install the following:
1.) eclipse java (client)
2.) eclipse jee (web service)
3.) jboss 5.1 AS
4.) soap ui, get the stand alone (http://www.soapui.org/)

For this example we will create a simple Calculator class.

Creating the web service:
1.) Open your eclipse-jee
2.) Create a new "Dynamic Web Project" and name it "WebServiceDemo", by clicking File->New->Dynamic Web Project
3.) Create a new class Calculator, see the ff class:
package com.kalidad.webservices;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class Calculator {
	@WebMethod
	public double add(double a, double b) {
		return a + b;
	}
	
	@WebMethod
	public double multiply(double a, double b) {
		return a + b;
	}
}
The @WebService tag, tells us that this class is a web service :-)
4.) Add the ff in your web.xml file located in WebContent/WEB-INF

		Calculator
		com.kalidad.webservices.Calculator
	
	
		Calculator
		/calc
	
It tells us that we can access the web service by typing /calc in the url.
5.) I assume you already setup jboss server, open the server view and Add Remove the newly created project to the Jboss 5.1
6.) Publish and start the server.
7.) Open the ff URL: http://localhost:8080/WebServiceDemo/calc?wsdl, you should see a page like:

Before we would create a java client to call the web service we would validate first if it's really working properly by using SoapUI.
1.) Run SoapUI.bat
2.) Create new SoapUI project.
3.) In the initial WSDL input type: http://localhost:8080/WebServiceDemo/calc?wsdl, click ok.
The sample Test Requests:
To test the sample requests, let's add some sample response
4.) Right click calc?wsdl and select New Test Suite.
5.) Right click test suite and select Add Test Case.
6.) Right click TestCase1, select Add Step->Mock Response
7.) Operation=add, Interface=CalculatorBinding, Port=8088, Path=/calc, checked Create Response
8.) Double click TestCase1, a panel will open in the right side and click the green triangle button.
9.) Now to run the request, Double click Request 1 and again click the green triangle button, you should see the reply now. Note that you can point the request to the mock service that we have or from the webservice that we created by changing the url in the top panel.
10.) If you want to change the value in the mock response, just double click "Mock Response" and change the return value.
Sample request:
2+2

2*2


Now we want to test our webservice by creating a java client.
1.) Generate the java classes, by invoking:
C:/jboss/jboss-5.1.0.GA/bin/wsconsume -o c:\test\calc -k http://localhost:8080/WebServiceDemo/calc?wsdl, it should create java and .class files in c:\test\calc directory.
2.) Run eclipse-java, and create a new java project, WebServiceDemoClient.
3.) You will notice, that there are compilation errors, just comment the code where it came from.
4.) Create a new class Client.
package com.kalidad.webservices;

public class Client {
	public static void main(String args[]) {
		new Client();
	}
	
	public Client() {
		CalculatorService service = new CalculatorService();
		Calculator calc = service.getCalculatorPort();
		double result = calc.add(2d, 4d);
		System.out.println("2 + 4 = " + result);
		
		result = calc.multiply(2d, 4d);
		System.out.println("2 * 4 = " + result);
	}
}
5.) Export the project as jar file by File->Export->Java->jar file. Just click next->next...->finish. Note I save the client.jar in c:\test folder.

6.) Now the setup is complete, we just have to run the client using wsrunclient. from jboss.
C:/jboss/jboss-5.1.0.GA/bin/wsrunclient -classpath "C:/jboss/jboss-5.1.0.GA/client/client/jbossall-client.jar;c:/test/client.jar" com.kalidad.webservices.Client
It should output:
And we're done :-).
Take note of the jboss directory, I didn't set it on the environment setting because I have several versions installed for different project so the path is hardcoded.

Related

java-testing 1834423864136955405

Post a Comment Default Comments

1 comment

Anonymous said...

Very usefull tutorial !

Thanks for the time used to write it !

item