Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JerseyTest is not compatible with JUnit 5 #3662

Open
jerseyrobot opened this issue Sep 6, 2017 · 9 comments
Open

JerseyTest is not compatible with JUnit 5 #3662

jerseyrobot opened this issue Sep 6, 2017 · 9 comments

Comments

@jerseyrobot
Copy link
Contributor

When using JerseyTest with JUnit 5 a null pointer exception gets thrown:

java.lang.NullPointerException
	at org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:564)
	at org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:578)

The issue seems to be that the setup() and tearDown() functions in the JerseyTest class are not called in JUnit 5 as they would be in JUnit 4. JUnit 5 uses the convention of @before and @after annotations.

Here is my workaround, in the test class that extends JerseyTest add the following two methods:

// do not name this setup()
@BeforeAll
public void before() throws Exception {
    super.setUp();
}

// do not name this tearDown()
@AfterAll
public void after() throws Exception {
    super.tearDown();
}
@jerseyrobot
Copy link
Contributor Author

@hanleyt Commented
Hey,
I created a JUnit 5 extension for using the JerseyTest framework if you'd like to check it out:
https://github.com/hanleyt/jersey-junit

Let me know if you guys would be interested in integrating this development.

Thanks,
Tomás

@jerseyrobot
Copy link
Contributor Author

@ValentinTrinque Commented
There is no need to worry. JUnit 5 ensures "Vintage" compatibility.

You juste have to import the JUnit 5 Vintage package:

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
</dependency>

Then, in your Jersey Test framework, just use JUnit 4 helpers:

package com.example.api.http;

import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;

// JUnit 4 Test helper
import org.junit.Test;

import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;

// JUnit 5 Assertions (!!!)
import static org.junit.jupiter.api.Assertions.assertEquals;

public class UsersResourceTest extends JerseyTest {
    @Override
    protected Application configure() {
        return new ResourceConfig()
            .register(UsersResource.class)
            .register(JacksonFeature.class);
    }

    @Test
    public void testGet() {
        Response response = target().path("users/1").request().get(Response.class);

        assertEquals(200, response.getStatus());
        assertEquals("{\"data\":{}}", response.readEntity(String.class));
    }
}

Note, you can use both JUnit 4 and 5 in the same test. Quite cool isn't.

@jerseyrobot
Copy link
Contributor Author

@hanleyt Commented
Thanks for the suggestion @ValentinTrinque. Indeed, it's very nice that JUnit 5 has backward compatability.

However it doesn't solve the issue. Yes, junit vintage allows you to run JUnit 4 tests on the JUnit 5 platform. And yes, you can use any assertion library you want (Truth, AssertJ, Junit4 or 5 etc). These aren't tied to any junit version/platform, they all just throw some sort of AssertionFailed exception when a condition isn't met.

However by continuing to use the JUnit 4 test API you lose out on the all the other nice JUnit 5 features, in particular the new extension model that allows multiple extensions to be applied, and for dependencies to be injected into methods/constructors. ( plus many other features)

Extending a class to get features isn't a great way to do things as of course you can only extend one class so inheritance should be avoided where possible.

@jerseyrobot
Copy link
Contributor Author

@agoncal
Copy link

agoncal commented May 17, 2019

I'm using Jersey 2.28 and JUnit 5.4.2 and I also have the NullPointerException. Any progress on the JUnit 5 support ?

@MaksymRybak
Copy link

I'm using Jersey 2.28 and JUnit 5.4.2 and I also have the NullPointerException. Any progress on the JUnit 5 support ?

Hello guys, same situation, any news? Thx!

@jonatan-ivanov
Copy link

Hi, Are there plans fixing this?

@izeye
Copy link

izeye commented Nov 9, 2021

This seems to be resolved via #4824.

@agoncal
Copy link

agoncal commented Nov 10, 2021

Yes, it looks like it's solving the issue. But you need to explicitelly exclude the JUnit 4 dependency (no matter which provider you use, grizzly2, inmemory, ...). So this is what my POM looks like:

    <dependency>
      <groupId>org.glassfish.jersey.test-framework.providers</groupId>
      <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
      <version>3.0.3</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>5.8.1</version>
      <scope>test</scope>
    </dependency>

I've migrated my tests to use JUnit 5, made sure I have no JUnit 4 dependency (by checking mvn dependency:tree) and it's working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants