GithubHelp home page GithubHelp logo

gertkommer / jersey-junit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hanleyt/jersey-junit

0.0 0.0 0.0 148 KB

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

License: Apache License 2.0

Java 87.41% Kotlin 12.59%

jersey-junit's Introduction

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.

jersey-junit's People

Contributors

hanleyt avatar tomashanley-toast avatar rmcdouga avatar snago avatar grunenwflorian avatar lordjaxom avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.