Skip to content

Instantly share code, notes, and snippets.

@psamsotha
Created December 27, 2015 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psamsotha/1edddc6f47785ed4f3b3 to your computer and use it in GitHub Desktop.
Save psamsotha/1edddc6f47785ed4f3b3 to your computer and use it in GitHub Desktop.
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