Skip to content

A JUnit 5 extension for testing JAX-RS and Jersey-based applications using the Jersey test framework.

License

Notifications You must be signed in to change notification settings

hanleyt/jersey-junit

Repository files navigation

Jersey-JUnit

A zero dependency JUnit 5 extension library for testing JAX-RS and Jersey-based applications using the Jersey test framework.

Build Status codecov Maven Central jitpack

Set Up

Add the following dependency to your gradle build file:

testCompile group: 'com.github.hanleyt', name: 'jersey-junit', version: '2.2.0'
// Add a test container factory, for example grizzly :
testCompile group: 'org.glassfish.jersey.test-framework.providers', name: 'jersey-test-framework-provider-grizzly2', version: '2.28'

The library is available from the maven central repository from version 2.1.0 onwards.

repositories { mavenCentral() }

If you need an earlier version you can find it on the jitpack repo.

repositories { maven { url 'https://jitpack.io/' } }

Note you must be using JUnit 5.1 or higher.

Usage

Register the extension in your test class programmatically using @RegisterExtension. This will start and stop the Jersey test container for each test.

You must pass the JerseyExtension constructor a supplier that returns a configured Application. If the ExtensionContext is required to configure the application, you can instead pass a function that accepts an extension context and returns a configured Application.

   @RegisterExtension
   JerseyExtension jerseyExtension = new JerseyExtension(this::configureJersey);

   private Application configureJersey(ExtensionContext extensionContext) {
       return new ResourceConfig(DummyResource.class);
   }

If the client configuration needs to be customized, then you can also pass in a function that accepts an ClientConfig and returns a modified ClientConfig object.

   @RegisterExtension
   JerseyExtension jerseyExtension = new JerseyExtension(this::configureJersey, this::configureJerseyClient);

   private Application configureJersey(ExtensionContext extensionContext) {
       return new ResourceConfig(DummyResource.class);
   }
   
   private ClientConfig configureJerseyClient(ExtensionContext extensionContext, ClientConfig clientConfig) {
   	clientConfig.connectorProvider(new ApacheConnectorProvider());
   	return clientConfig;
   }
   
   

You can then inject the WebTarget, Client or base URI as test method or constructor parameters.

   @Test
   void web_target_is_injected(WebTarget target, Client client, URI baseUri) {
      assertThat(target).isNotNull();
      String values = target.path("values").request().get(String.class);
      assertThat(values).isEqualTo(DummyResource.DEFAULT_VALUES);
   }

See the JerseyExtensionTest for more usage examples.

About

A JUnit 5 extension for testing JAX-RS and Jersey-based applications using the Jersey test framework.

Resources

License

Stars

Watchers

Forks

Packages

No packages published