Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running JUnit Tests on a Restlet Router

Using Restlet I have created a router for my Java application.

From using curl, I know that each of the different GET, POST & DELETE requests work for each of the URIs and return the correct JSON response.

I'm wanting to set-up JUnit tests for each of the URI's to make the testing process easier. However, I'm not to sure the best way to make the request to each of the URIs in order to get the JSON response which I can then compare to make sure the results are as expected. Any thoughts on how to do this?

like image 572
Lee Avatar asked Feb 22 '10 15:02

Lee


1 Answers

You could just use a Restlet Client to make requests, then check each response and its representation.

For example:

Client client = new Client(Protocol.HTTP);
Request request = new Request(Method.GET, resourceRef);
Response response = client.handle(request);

assert response.getStatus().getCode() == 200;
assert response.isEntityAvailable();
assert response.getEntity().getMediaType().equals(MediaType.TEXT_HTML);

// Representation.getText() empties the InputStream, so we need to store the text in a variable
String text = response.getEntity().getText();
assert text.contains("search string");
assert text.contains("another search string");

I'm actually not that familiar with JUnit, assert, or unit testing in general, so I apologize if there's something off with my example. Hopefully it still illustrates a possible approach to testing.

Good luck!

like image 120
Avi Flax Avatar answered Oct 18 '22 14:10

Avi Flax