使用 Jersey 测试框架测试 Jersey 3 Web 应用程序的问题

问题描述

我有一个 Jersey 3 Web 应用程序(在 Tomcat10、JDK15 和 junit5 上运行),我想在其中对我的端点进行单元测试。

虽然,我在设置这些时遇到了麻烦。我的理解是,我需要使用 GrizzlyWebTestContainer 才能获得正确的环境设置。另外,我想我需要以某种方式将我的 web.xml 传递给 ServletcContainer。但是我该怎么做呢?

当我运行测试时,我得到:

java.lang.NullPointerException: Cannot invoke "org.glassfish.jersey.test.spi.TestContainer.getBaseUri()" because the return value of "org.glassfish.jersey.test.JerseyTest.getTestContainer()" is null

所以我还需要在某处配置 baseUri - 但在哪里?不幸的是,我只找到了 GrizzlyTestContainerFactory 而不是 GrizzlyWebTestC 的示例和文档... 有人能解释一下我错过了什么吗?

非常感谢任何输入!

这是我的单元测试:

public class SimpleTest extends JerseyTest {

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


    @Override
    public DeploymentContext configureDeployment() {
        return ServletDeploymentContext
                .forServlet(new ServletContainer()) //how to pass web.xml here?
                .build();
    }

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

    @Test
    public void test() {
        Response response = target("/data/180").request().get(); //--> NPE is thrown here
        assertEquals(200,response.getStatus());
    }
}

这是我的端点,我想测试:

@Path("/data")
public class DataController {

    @Context ServletContext context;

    private DataSource ds;

    @postconstruct
    public void init() { ds = (DataSource) context.getAttribute(DATASOURCE_CONTEXT_NAME); }

    @GET
    @Path("/{dataId}")
    @Produces(MediaType.APPLICATION_JSON)
    @RolesAllowed({"1","2"})
    public Response getDataById(@PathParam("id") Long id) throws sqlException {
        logger.debug("Fetching dataset with id " + id);
        DSLContext create = DSL.using(ds.getConnection());

            //some JOOQ code

            return Response.ok(dto).build();
        }
}

这是我的 pom.xml 中最重要的部分

<dependencies>
        <!-- Testing -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework</groupId>
            <artifactId>jersey-test-framework-core</artifactId>
            <version>3.0.1</version>
            <scope>test</scope>
        </dependency>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)