-
Notifications
You must be signed in to change notification settings - Fork 351
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
Comments
@hanleyt Commented Let me know if you guys would be interested in integrating this development. Thanks, |
@ValentinTrinque Commented You juste have to import the JUnit 5 Vintage package:
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. |
@hanleyt Commented 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. |
|
I'm using Jersey 2.28 and JUnit 5.4.2 and I also have the |
Hello guys, same situation, any news? Thx! |
Hi, Are there plans fixing this? |
This seems to be resolved via #4824. |
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,
I've migrated my tests to use JUnit 5, made sure I have no JUnit 4 dependency (by checking |
When using JerseyTest with JUnit 5 a null pointer exception gets thrown:
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:
The text was updated successfully, but these errors were encountered: