13

Is there any implementation of javax.ws.rs.core.UriInfo which I can use to create an instance quickly for testing. This interface is long, I just need to test something. I don't want to waste time on whole implementation of this interface.

UPDATE: I want to write a unit test for a function similar to this:

@GET
@Path("/my_path")
@Produces(MediaType.TEXT_XML)
public String webserviceRequest(@Context UriInfo uriInfo);
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Aqeel Ashiq
  • 1,988
  • 5
  • 24
  • 57

3 Answers3

14

You simply inject it with the @Context annotation, as a field or method parameter.

@Path("resource")
public class Resource {
    @Context
    UriInfo uriInfo;

    public Response doSomthing(@Context UriInfo uriInfo) {

    }
}

Other than your resource classes, it can also be injected into other providers, like ContainerRequestContext, ContextResolver, MessageBodyReader etc.

EDIT

Actually I want to write a junit test for a function similar to your doSomthing() function.

I didn't pick that up in your post. But a couple options I can think of for unit tests

  1. Simply create a stub, implementing only the methods you use.

  2. Use a Mocking framework like Mockito, and mock the UriInfo. Example

    @Path("test")
    public class TestResource { 
        public String doSomthing(@Context UriInfo uriInfo){
            return uriInfo.getAbsolutePath().toString();
        }
    }
    [...]
    @Test
    public void doTest() {
        UriInfo uriInfo = Mockito.mock(UriInfo.class);
        Mockito.when(uriInfo.getAbsolutePath())
            .thenReturn(URI.create("http://localhost:8080/test"));
        TestResource resource = new TestResource();
        String response = resource.doSomthing(uriInfo);
        Assert.assertEquals("http://localhost:8080/test", response);
    }
    

    You'll need to add this dependency

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.0</version>
    </dependency>
    

If you want to do an integration test, where the actual UriInfo is injected, you should look into Jersey Test Framework

Here's a complete example with the Jersey Test Framework

public class ResourceTest extends JerseyTest {

    @Path("test")
    public static class TestResource {
        @GET
        public Response doSomthing(@Context UriInfo uriInfo) {
            return Response.ok(uriInfo.getAbsolutePath().toString()).build();
        }
    }

    @Override
    public Application configure() {
        return new ResourceConfig(TestResource.class);
    }

    @Test
    public void test() {
        String response = target("test").request().get(String.class);
        Assert.assertTrue(response.contains("test"));
    }
}

Just add this dependency

<dependency>
    <groupId>org.glassfish.jersey.test-framework.providers</groupId>
    <artifactId>jersey-test-framework-provider-inmemory</artifactId>
    <version>${jersey2.version}</version>
</dependency>

It uses an in-memory container, which is the most efficient for small tests. There are other containers with Servlet support if needed. Just see the link I posted above.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Actually I want to write a junit test for a function similar to your doSomthing() function. – Aqeel Ashiq Apr 24 '15 at 09:54
  • 1
    Ahh I missed that part. You can use a Mocking framework like Mockito, or just create a stub, implementing only the methods you are using. That's the only thing I can think of for a unit test. For an integration test, you could use the Jersey Test Framework, and it will launch the app and be fully functional for the tests (injections included) – Paul Samsotha Apr 24 '15 at 09:57
0

You either mock it, or use something like http://arquillian.org/

Friso
  • 1,080
  • 6
  • 37
0

I was writing integration tests, so cannot use mock stuff

I used some code for jersey tests

http://www.programcreek.com/java-api-examples/index.php?source_dir=JerseyTest-master/jersey-tests/src/test/java/com/sun/jersey/impl/uri/UriPathHttpRequestTest.java

WebApplicationImpl wai = new WebApplicationImpl();
ContainerRequest r = new TestHttpRequestContext(wai,
            "GET", null,
            "/mycontextpath/rest/data", "/mycontextpath/");
UriInfo uriInfo = new WebApplicationContext(wai, r, null);
myresources.setUriInfo(uriInfo);

and

private static class TestHttpRequestContext extends ContainerRequest {
    public TestHttpRequestContext(
            WebApplication wa,
            String method,
            InputStream entity,
            String completeUri,
            String baseUri) {
        super(wa, method, URI.create(baseUri), URI.create(completeUri), new InBoundHeaders(), entity);
    }
}

If you get any errors about request scope beans see request scoped beans in spring testing

Community
  • 1
  • 1
Kalpesh Soni
  • 6,879
  • 2
  • 56
  • 59