How to test JERSEY controller methods with UriInfo

jakarta-ee, java, jersey, unit-testing

Solution

`UriInfo` is an interface so you can't create its object directly. You need to subclass it and create your own `UriInfo` class. So your uriinfo class should convert String uri/url into UriInfo object.

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

So this way you can unit test your resources without running it inside tomcat/server.

Problem

I am writing unit test for a JERSEY project. For methods with no query string , I can just instantiate controller and call the method. Also work for argument in the path because they appear as string arguments of the method. But when I get queryStrings the mode has a special argument `(@Context UriInfo url)` How can I build the UriInfo argument in my unit tests ? Why does this class has no constructor ?

Original source