Skip to content

Instantly share code, notes, and snippets.

@seratch
Created July 13, 2011 14:26
Show Gist options
  • Save seratch/1080390 to your computer and use it in GitHub Desktop.
Save seratch/1080390 to your computer and use it in GitHub Desktop.
JerseyTest example with Spring
/*
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly2</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
*/
package restful.server.resource;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
import com.sun.jersey.test.framework.AppDescriptor;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;
import javax.ws.rs.core.MultivaluedMap;
import org.junit.Test;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.request.RequestContextListener;
public class SomeResourceTest extends JerseyTest {
@Override
protected AppDescriptor configure() {
return new WebAppDescriptor.Builder("restful.server.resource")
.contextParam("contextConfigLocation", "classpath:/applicationContext.xml")
.contextPath("/").servletClass(SpringServlet.class)
.contextListenerClass(ContextLoaderListener.class)
.requestListenerClass(RequestContextListener.class)
.build();
}
@Test
public void searchAPI() throws Exception {
WebResource webResource = resource();
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
params.add("q", "Java");
SearchResultResponse response = webResource.path("/search/")
.queryParams(params)
.header("Authorization", "...")
.get(String.class);
System.out.println(response.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment