Sachin Sachin - 1 year ago 143
Java Question

Using PATCH with Jersey Client API for unit testing

I am working on a REST API implementation using Jersey. For

(partial updates), I have implemented my own custom implementation of
since Jersey does not support it.

Now I am trying to figure out how to write functional tests around that implementation. I am using jersey test framework for other methods (
) that has that support available in that framework.

Is there a way where in I can extend jersey test framework implementation to write my functional tests for
If not, are there any other test frameworks available that I can use to test my Jersey

If anyone can provide any examples, that would be great.

Answer Source

Assuming your implementation consists of a custom annotation like this

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public @interface PATCH {}

Trying to do something like this with the Client

 String response = target.request().method("PATCH", Entity.text("Hello"), String.class);

by default is not supported, and will an exception like Invalid HTTP method: PATCH

This is not a problem with the Client API directly, but with the lower level Java APIs. Seems to be some security restriction.

With the Client API we can override this by setting a property

In the JerseyTest, one way to configure the Client is to override configureClient, and set the property with the ClientConfig. You could just as easily set the property on the Client itself, but staying in the spirit of the JerseyTest framework (where we don't need to explicitly access the Client, the example below will just just override the method

public class PatchTest extends JerseyTest {

    public static class PatchResource {
        public String getPatch(String request) {
            return "Patched " + request;

    protected void configureClient(final ClientConfig config) {, true);

    public Application configure() {
        return new ResourceConfig(PatchResource.class);

    public void doPatchTest() {
        WebTarget target = target("patch");
        String response = target.request().method("PATCH", Entity.text("Hello"), String.class);
        Assert.assertEquals("Patched Hello", response);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download