Skip to content

A JUnit 5 extension which allows running unit tests against a SeBootstrap instance

License

Notifications You must be signed in to change notification settings

resteasy/resteasy-junit-extension

Jakarta REST SeBoostrap JUnit 5 Integration

Table of Contents

This is a simple project which allows a unit testing for Jakarta REST endpoints using the SeBootstrap from Jakarta REST 3.1.

Usage

To use this JUnit 5 integration you simply need to add a @RestBootstrap(YourApplication.class) annotation to your test. A SeBoostrap.Instance will be created and started based on the implementation you choose.

Dependencies

The first dependency you need is the testing tooling.

<dependencies>
    <dependency>
        <groupId>dev.resteasy.testing.tools</groupId>
        <artifactId>junit-testing-tools</artifactId>
        <version>${version.dev.resteasy.testing.tools</version>
    </dependency>
</dependencies>

Next you need to define an implementation. This project chose not to be implementation specific and should work with any Jakarta REST 3.1+ implementation.

RESTEasy Example
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-bom</artifactId>
            <version>${version.org.jboss.resteasy}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>${version.org.junit}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-core-spi</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-client</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-undertow-cdi</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Examples

Simple Test
@RestBootstrap(value = SimpleTest.TestApplication.class)
public class SimpleTest {

    @Inject
    @RequestPath("/test/echo")
    private WebTarget webTarget;

    @Test
    public void invokeResource(final UriBuilder builder) {
        try (Client client = ClientBuilder.newClient()) {
            final String result = client.target(builder.path("/test/echo/"))
                    .request()
                    .post(Entity.text("Hello"), String.class);
            Assertions.assertEquals("Hello", result);
        }
    }

    @Test
    public void invokeResourceOnInjectedClient() {
        final String result = webTarget.request()
                    .post(Entity.text("Hello"), String.class);
        Assertions.assertEquals("Hello", result);
    }

    @ApplicationPath("/test")
    public static class TestApplication extends Application {
        @Override
        public Set<Class<?>> getClasses() {
            return Set.of(EchoResource.class);
        }
    }
    @Path("/echo")
    public static class EchoResource {
        @POST
        public String echo(String text) {
            return text;
        }
    }
}

Injection

You can inject some types into your tests. For fields you use the @jakarta.inject.Inject annotation. Constructor and method parameters do not require the @Inject annotation. The following types can be injected.

  • jakarta.ws.rs.SeBootstrap.Configuration

    The configuration from the SeBoostrap.Instance that was started.

  • jakarta.ws.rs.client.Client

    A REST Client. This can have a qualifier of @RestClientConfig which returns a RestClientBuilderProvider and allows the configuration to be overridden for the client.

  • jakarta.ws.rs.core.UriBuilder

    Injects a URI builder. Given this is a mutable builder, this should likely only be injected as a method parameter.

  • java.net.URI

    The base URI for SeBootstrap.Instance that was started.

  • jakarta.ws.rs.client.WebTarget

    This can be used with the @RequestPath qualifier. It creates a WebTarget from a configured client.

Type Field Parameter Constructor Qualifiers

jakarta.ws.rs.SeBootstrap.Configuration

X

X

X

jakarta.ws.rs.client.Client

X

X

X

  • @RestClientConfig (optional)

jakarta.ws.rs.core.UriBuilder

X

X

java.net.URI

X

X

X

jakarta.ws.rs.client.WebTarget

X

X

X

  • @RequestPath (optional)

  • @RestClientConfig (optional)

About

A JUnit 5 extension which allows running unit tests against a SeBootstrap instance

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages