how to write unit tests for REST web services developed using apache-cxf with spring without using Maven - unit-testing

I have developed a REST web application using apache cxf library. I am able to access them using a browser and test. I want to write unit tests for the services.
I tried testing it using embedded Jetty server with cxf.
After starting the Jetty server, I am getting a proxy using WebClient API of cxf. The relevant code is
WebClient client = WebClient.create(ENDPOINT_ADDRESS);
client.accept("text/xml");
client.path("/studyservice/topimstudydetails");
client.replaceQuery("pnlId=3&lclId=30&indId=442&maxImStudies=99&rName=DEV");
TopIMStudyDetailsResponse resp = client.get(TopIMStudyDetailsResponse.class);
assertEquals(resp.getStatus().getReturnCode(),0);
The call to the service is successful as I can see the logs, but I'm get a null pointer where the service is trying to make a dao call. The dao is declared as a dependency for the service. How can we get a proxy with all the dependencies injected ?

May be you'll find the following posts helpful:
http://tarlogonjava.blogspot.co.il/2011/12/running-integration-test-using-with.html
http://tarlogonjava.blogspot.co.il/2011/12/running-integration-tests-using-with.html
The second post is about replacing the data source with a special in-memory data source for tests.

Related

Is there a way to publish a Spring web-service endpoint only for a specific environment?

There is a web-application that uses WebServiceTemplate from spring-ws to consume remote SOAP-based web-services.
I would like to create a web-service endpoint ("mocking" the remote endpoints) so that it can be used when the remote web-service is down. However, I want this "mocked" web-service to be reachable only from the environment used for development, it shouldn't be published in other environments (e.g.: Integration, Test, Production, etc.).
The current environment can be determined from a configuration-entry.
Is there a way I can publish a web-service with spring in such a way?
If you want to create this mock object only in dev environment then you should use Spring's #Conditional annotation. Here's an example.

SoapUI: Create mock service with pass-through behavior for selected methods

While developing a web application I have the following use case:
a 3rd party Web Service with quite a lot of methods is deployed on a test server A (with a single endpoint, e.g. http://3rdPartyServer/3rdPartySvc?WSDL)
a new method is about to be implemented in the near future, but I need to use it now
the rest of the methods are used throughout my code extensively
So I would like to do the following:
Create a mock service in SoapUI locally, based on the new WSDL which includes the new WS method (i.e. a superset of the WS methods currently on server A)
point my local application server to use the SoapUI mock service endpoint
mock only the response of the new WS method (create a dummy response for it in SoapUI)
let the other WS method calls to reach server A and return whatever it returns normally (i.e. use SoapUI as a proxy for these calls)
I've gone through the SoapUI documentation regarding service mocking and have used it numerous times, but could not find an option for such "pass-through" behavior.
When you read in your WSDL, the endpoint will point to your server.
Open your service, and select the service endpoint.
Add a second endpoint, to point to your mock. SoapUI has little bit of documentation showing this here. Only step "2. Getting Started" applies, not step 3!
In each of your tests, where you are using the mocked method, you will need to select the mock endpoint. Further discussion is here.

Use CXF client api with Java web application

How to consume Web service suing Apache CXF client API.
I have generated the client Code using eclispe but I didn't found any document specifying how to use that generated code in my web application.
How to configure CXF? I am using tomcat to run my java web appliation.
How to use the generated code?
Do I need to add anyhting in my my web.xml?
I have downloaded CXF binaries from apache CXF website but don't know which libraries are needed. I am affraid i may end up adding all the jars.
I am using Tomcat 7, Java 1.6 and plane jsp/Servlet for my application
I am new to web services.
Thanks in advance
One sample code that may help.
URL wsdlurl=SOAPWebServiceTransport.class.getClassLoader().
getResource("my.wsdl");
// MyService will be one of the service class that you have generated (with different name ofcourse)and which must be extending Service class
//getOnlineServicePort will be a method (with different name ofcourse) in your service class which will give you the interface referrer using which you'll call the webservice method
OnlinePort service= new MyService(wsdlurl).getOnlineServicePort();
Client proxy = ClientProxy.getClient(service);
//configure security user name password as required
//Add interceptors if needed
//Now you can directly call the webservice methods using the service object
service.method(parameter)
you can also refer to some example one is here

How to test multiple Web Services with one script using UFT?

I am trying to create a set of Regression Test scripts to test the different Web Services and the corresponding methods related to my Web Application in HP UFT. I am using the API testing feature of UFT. I have the request and response XMLs (obtained manually) of each web service method necessary for the testing. There are more than 50 web service methods to be tested. The test cases for all of them are same. The input data of the request is currently being set by parameterizing the data from an excel spreadsheet.
Since the test cases are exactly same and the input data for all the web service methods have similar fields, such as First Name, Last Name, LoginID etc., I want to know if there is a way to write a single script which dynamically reads the web service method name and the associated data from the spreadsheet and uses it in the request XML file (same name as the web service method) which is also loaded in run time.
In this way, after the script has been developed, all I need to do prepare a list of all the web service methods to be tested and enter them in the Test spreadsheet along with the required request data. The script would loop through all the web service methods and execute the test cases for each web service method one by one.
in your case I think you should use a Soap Request method rather than a Web Service Call. Using soap request, other than the message itself, you can parameterize also the Endpoint Address and the SoapAction
Hope this helps
Yossi

Consuming WebService from Web Service Client

I'm trying to consume a web service using Eclipse. I created a Web Service Client from the following wsdl: https://www.esnfs.com.br:8444/enfsws/services/Enfs?wsdl
Using this, I generated 5 classes:
Enfs.java
EnfsLocator.java
EnfsPortType.java
EnfsPortTypeProxy.java
EnfsSoap11BindingStub.java
I don't know how to use these classes. How do they work? I need some help using these classes, along with consuming the Web Services.