javajbosstestngjax-rsjersey-test-framework

Jersey test framework: Test Container is null


I have the following test class that tests a simple "/helloWorld" endpoint that is implemented in jax-rs. I am running this as an EAR file in a JBOSS EAP Server locally.

public class HelloWorldTest extends JerseyTest {


    @Override
    protected Application configure() {
        return new ResourceConfig(HelloWorldEndpoint.class);
    }

    @Override
    public TestContainerFactory getTestContainerFactory() {
        return new GrizzlyWebTestContainerFactory();
    }

    @Test
    public void shouldGetApplyStreams() {

        Response response = target("/helloWorld").request()
                .get();

        assertEquals("Http Response should be 200: ", Response.Status.OK.getStatusCode(), response.getStatus());
    }
}

When I run the test I get the following:

java.lang.NullPointerException
    at org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:565)
    at org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:579)
    at com.hrlogix.ats.http.unsecured.ApplyFlowEndpointTest.shouldGetApplyStreams(HelloWorldEndpointTest.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
    at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110)

Solution

  • JerseyTest is not compatible with JUnit 5 at the moment.

    https://github.com/javaee/jersey/issues/3662

    From their workaround, add

    @TestInstance(Lifecycle.PER_CLASS)
    

    to your class, and these 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();
    }