Git Product home page Git Product logo

resteasy-junit-extension's Introduction

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)

resteasy-junit-extension's People

Contributors

jamezp avatar dependabot[bot] avatar liweinan avatar

Stargazers

 avatar

Watchers

jimma avatar  avatar  avatar Ron Sigal avatar

resteasy-junit-extension's Issues

Allow for a `WebTarget` to be injected

A WebTarget can be injected with an annotation like @RequestPath("/some/path") which would effectively be a short-cut for inject a Client and a UriBuilder and building the WebTarget manually.

The @RequestPath might be a pseudo qualifier for @Inject or it could just be a standalone annotation for injection. The qualifier feels a bit odd since this is not real CDI. However, it keeps a little consistency with the rest of the injection points.

Along with this, we may want to allow for a Invocation to be injected as well. This would again be a short-cut for WebTarget.request(). A potential qualifier could be used to set the accepted media types.

Should the `UriBuilder` injection be deprecated for fields

The UriBuilder is mutable and makes it a bit odd to work with when injecting it into fields. Once something like UriBuilder.path("/some/path") is invoked, the builder is mutated. For this reason it might make since to only allow for parameters to be injected into method Parameters and not allow in constructor parameters or in fields.

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.