In our JAX-RS webservice using CXF tutorial, we have created a restful webservice that contains two methods. One method accepts text type input while the second one accepts a JsonBean type.
Remember, in our test case, HelloAngryWorldTest.java, by default, when the file was created using maven, there were two methods that tests both text and JsonBean return types. It is modified it a bit to have an extra method as below:
1. HelloAngryWorldTest.java :
loading...
In Apache CXF, there is a helper class called
org.apache.cxf.jaxrs.client.WebClient
. This helper class is used to create the client. This class reduces the code to create a response to just two lines (see testPing()
method). Without this helper class, we would need to create a lot of objects.An example restful client with(see,
testPing()
) and without(see, testPingUsingPureJaxRS()
) using the helper class are shown in the above example. The third method, testJsonRoundtrip()
demonstrates usage of jsonBean return type.testPingUsingPureJaxRS()
method uses pure JAX-RS classes for creating the response. Using pure JAX-RS classes the response creation will be as follows :Client -> WebTarget -> Invocation.Builder -> Response
Using Apache CXF helper class the reponse creation becomes as simple as below :
WebClient -> Response
There are number of ways you can test the Restful webservice. Before running any of the below, make sure the tomcat server is started and the webservice is running using,
mvn clean package -DskipTests=true tomcat7:run
or mvn clean package -DskipTests=true jetty:run
1. Using maven command :
i. Specify the service.url at command line :
mvn -Dservice.url=http://localhost:<port-number>/jaxrs-service test
This command uses a command line argument to specify the service.url. The service.url is used in the test case.
ii. Specify the service.url in pom.xml
loading...
Add the above configuration and use the below command to test the webservice,
mvn test
iii. Hard-code the service.url in HelloWorldTest.java and use the below command to run the tests.
mvn test
Output:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.learninjava.HelloAngryWorldTest
Response from web service is : Birds
Response from web service is : Tweet
Response from web service is : Tweet
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 41.107 sec
Results :
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.learninjava.HelloAngryWorldTest
Response from web service is : Birds
Response from web service is : Tweet
Response from web service is : Tweet
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 41.107 sec
Results :
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
2. Using Poster plugin :
Poster is a plugin for firefox browser. This is a very useful and handy tool to test your restful webservices quickly.
i. To test the ping method of HelloAngryWorld, enter the following in Poster UI :
URL: http://localhost:<port-number>/jaxrs-service/helloAngryWorld/echo/tweet
Content Type: application/text
and click on GET button as ping is a GET request type. The response is displayed in the Response window.
ii. To test the
modifyJson
method of HelloAngryWorld, enter the following in Poster UI :URL: http://localhost:<port-number>/jaxrs-service/helloAngryWorld/jsonBean
Content Type: application/json
Payload(Text Box at bottom): { "val1": "Birds", "val2": "Eggs" }
and click on POST button as modifyJson is a POST request type. The response is displayed in the Response window.
Thats all folks !! Happy coding. If you feel this helped you, keep supporting us by or or below or on the articles on social media.