How to test JERSEY controller methods using UriInfo

I am writing unit test for a JERSEY project.

For methods without a query string, I can simply create an instance of the controller and call the method.

Also work for the argument in the path, because they appear as string arguments to the method.

But when I get queryStrings, the mode has a special argument (@Context UriInfo url)

How can I build a UriInfo argument in my unit tests? Why doesn't this class have a constructor?

+5
source share
2 answers

UriInfois an interface, so you cannot create it directly. You need to subclass it and create your own class UriInfo. So your uriinfo class should convert String uri / url to a UriInfo object.

public class UriInformation implements UriInfo {
    MultivaluedMap<String, String> pathParamMap;
    MultivaluedMap<String, String> queryParamMap;
    public UriInformation(UriInfo uriInfo) {
        //parse uriInfo 
    }
 //setters/getters
}

, unit test , tomcat/server.

+2

Mockito , UriInfo:

import java.net.URI;
import javax.ws.rs.core.UriInfo;

UriInfo mockUriInfo = mock(UriInfo.class);
when(mockUriInfo.getRequestUri()).thenReturn(new URI("http://www.test.com/go"));
+6

All Articles