gpt4 book ai didi

java - Jersey 测试框架: Test Container is null

转载 作者:行者123 更新时间:2023-12-01 16:35:31 28 4
gpt4 key购买 nike

我有以下测试类,用于测试在 jax-rs 中实现的简单 "/helloWorld" 端点。我将其作为 EAR 文件在本地 JBOSS EAP 服务器中运行。

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

当我运行测试时,我得到以下结果:

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 目前与 JUnit 5 不兼容。

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

从他们的解决方法中,添加

@TestInstance(Lifecycle.PER_CLASS)

到你的类(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();
}

关于java - Jersey 测试框架: Test Container is null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61957042/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com