简体   繁体   English

Jersey 测试框架:测试容器为 null

[英]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 .我有以下测试 class 测试在jax-rs中实现的简单"/helloWorld"端点。 I am running this as an EAR file in a JBOSS EAP Server locally.我在本地JBOSS EAP服务器中将其作为 EAR 文件运行。

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)

JerseyTest is not compatible with JUnit 5 at the moment. JerseyTest 目前与 JUnit 5 不兼容。

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

From their workaround, add从他们的解决方法中,添加

@TestInstance(Lifecycle.PER_CLASS)

to your class, and these two methods:到你的 class,这两种方法:

// 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM