Skip to main content

Hey guys,

I was just working on the Foodeys project, which is a Java EE 7 demo project by the [+JUG Cologne](http://plus.google.com/104855103747757515618), and I built up the REST-Endpoints with JAX-RS 2.0.
As I always use [+Arquillian](http://plus.google.com/100660127586085393031) whenever I can, I also wanted to try the new JAX-RS 2.0 Client API and integrate it into my Arquillian tests.
To come straight to the point: It was so damn easy!

Here is a [+GitHub](http://plus.google.com/108056891227336299381) Gist to show yeah all relevant parts: https://gist.github.com/w0mbat/6317015

Lets talk about the details:
The method „testAutocompleteWithoutQuery“ has an Annotation called „@RunAsClient“ which tells Arquillian to run the Test from a client perspective. This makes sense, as we want to communicate with a REST Endpoint.
Nice hint by [+Aslak Knutsen](http://plus.google.com/118222277307713726144): If our test class only consists of client tests, then we can skip adding „@RunAsClient“ to each of the test cases and change „@Deployment“ to „@Deployment(testable=false)“. The tests will run the same but it skips the bundling chain and remove overhead.

The parameter „@ArquillianResource URL base“ tells Arquillian, to inject the current base URL to our deployment.
In the first line, we create a new JAX-RS 2.0 Client. Then we tell the client which target (REST endpoint) it has (base URL + REST path + Resource context).
In the third line we tell the client to do a GET request and we also save the response coming back. Last but not least we do an assert on the response code we expect: 400 a.k.a. „Bad Request“.

Once we try this, we will get the following error: „java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder“. This is because we didn´t package the jars for JAX-RS 2.0 Client into our test cases. Within the gist, second file, you can see a dependency „resteasy-client“ with scope „test“. Once this is added, our test runs smoothly (Tested on current Glassfish 4 and JBoss Wildfly).

Happy Testing!

Daniel Sachse

Author Daniel Sachse

More posts by Daniel Sachse