Layman
Layman

Reputation: 1056

How to initialize JAX-RS HttpHeaders

I have an internal framework with a class that looks like this:

public abstract class POSEnterpriseResource extends SynchronousResource {
  ...
  @Inject
  protected HttpHeaders headers;
  ...
}

And I need to write unit tests for a class that extends it to use code like

String acceptMediaType = headers.getHeaderString("Accept");
String acceptVersion = headers.getHeaderString("Accept-Version");

The HttpHeaders type in question is an interface under javax.ws.rs.core.HttpHeaders and does not expose any initialization methods, only getters. I do not get to touch the existing code.

How do I mock a headers object like this without setting up a whole ResponseEntity?

Upvotes: 3

Views: 5167

Answers (2)

Ed Bighands
Ed Bighands

Reputation: 169

Using Mockito, you can mock the behaviour of the getX methods to return stubs exactly as you want. I find Paul's answer complete enough. Also, mockito.mock actually allows you to mock Interfaces.

Upvotes: 1

Paul Samsotha
Paul Samsotha

Reputation: 208994

You can just use a mocking library like Mockito. Here's how to set up the test

// set up the runner so Mockito handles all the 
// initialization and injections
@RunWith(MockitoJUnitRunner.class)
public class Testing {

    // HttpHeaders is mocked and injected into test
    @Mock
    private HttpHeaders headers;

    // HttpHeaders is injected into the resource class field
    @InjectMocks
    private TestResource resource;

    @Test
    public void testHttpHeaders() {
        // control what the mock returns when certain methods are called
        when(headers.getHeaderString("X-Test")).thenReturn("X-Test-Value");
    }
}

And below is a complete test to show you the general way to testing using the library. If you've never used the library before, I suggest taking some time to go through the docs. It's pretty easy to learn the basics.

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.HttpHeaders;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;


@RunWith(MockitoJUnitRunner.class)
public class Testing {

    @Mock
    private HttpHeaders headers;

    @InjectMocks
    private TestResource resource;

    @Test
    public void testHttpHeaders() {
        when(headers.getHeaderString("X-Test")).thenReturn("X-Test-Value");

        String response = resource.get();
        assertThat(response).isEqualTo("X-Test-Value");
    }


    @Path("test")
    public static class TestResource {

        @Inject
        private HttpHeaders headers;

        @GET
        @Produces("text/plain")
        public String get() {
            return headers.getHeaderString("X-Test");
        }
    }
}

Upvotes: 5

Related Questions