Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test class using native Jersey capabilites
import java.util.logging.Logger;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import static junit.framework.Assert.assertEquals;
/**
* Run this test class like any other JUnit test. It makes use of Jersey Test
* Framework. You will need to following dependency to run it.
*
* <dependency>
* <groupId>org.glassfish.jersey.test-framework.providers</groupId>
* <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
* <version>${jersey2.version}</version>
* <scope>test</scope>
* </dependency>
*/
public class NativePropsTest extends JerseyTest {
private static final String CONFIG_PROP = "property";
private static final String PROP_VALUE = "PropertyValue";
@Path("config")
public static class ConfigResource {
@Context
private Configuration configuration;
@GET
public String getConfigProp() {
return (String)configuration.getProperty(CONFIG_PROP);
}
}
@Override
public ResourceConfig configure() {
ResourceConfig config = new ResourceConfig(ConfigResource.class);
config.register(new LoggingFilter(Logger.getAnonymousLogger(), true));
config.property(CONFIG_PROP, PROP_VALUE);
return config;
}
@Test
public void should_return_correct_property_value() {
Response response = target("config").request().get();
assertEquals(200, response.getStatus());
assertEquals(PROP_VALUE, response.readEntity(String.class));
response.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment