Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST Resource Testing

Tags:

rest

testing

I want to test a REST Resource:

 @GET
@Path("/{dbName}")
@Produces(MediaType.APPLICATION_JSON)
public String getRequest(@PathParam("dbName") String dbName)
        throws JSONException  {

}

I want to do this :

assert(someExpectedOutput,getRequest())

Now How the PAth will be set for @Path annotation ??

like image 800
rm21 Avatar asked Jun 23 '26 21:06

rm21


2 Answers

If you are going to call getRequest directly for testing, as a Java method, go right ahead and pass it a single string:

assertEquals(someExpectedOutput, getRequest("someMockDbName"));

It is after all, just a Java method with one string parameter.

The JAX-RS annotations only kick in when you are running a server, in which case the framework will match the path parameter to your Java parameter dbname. I assume for testing you are just going to use mock objects, so no worries. Pass whatever string you like.

Now if you are going to test within a server (more like an integration test), consider something like Jetty for the webserver and HSQLDB or Derby for a database. That kind of testing is much more involved.

like image 98
Ray Toal Avatar answered Jun 26 '26 20:06

Ray Toal


Found Jersey framework for Testing (com.sun.jersey.test.framework) that is useful too. or do it the normal junit way as above.

like image 40
rm21 Avatar answered Jun 26 '26 20:06

rm21



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!