简体   繁体   English

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

[英]Problems with Testing Jersey 3 Web Application using Jersey Test Framework

I've got a Jersey 3 Web application (running on Tomcat10, JDK15 with JUnit5) in which I would like to Unit test my Endpoints.我有一个 Jersey 3 Web 应用程序(在 Tomcat10、JDK15 和 JUnit5 上运行),我想在其中对我的端点进行单元测试。

Although, I have troubles setting these up.虽然,我在设置这些时遇到了麻烦。 What I understand is, that I need to use a GrizzlyWebTestContainer in order to get the correct environment setup.我的理解是,我需要使用 GrizzlyWebTestContainer 才能获得正确的环境设置。 Also, I figure I need somehow to pass my web.xml to the ServletcContainer.另外,我想我需要以某种方式将我的 web.xml 传递给 ServletcContainer。 But how would I do this?但是我该怎么做呢?

When I run the test, I get:当我运行测试时,我得到:

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

So I need to configure somewhere a baseUri as well - but where?所以我也需要在某个地方配置一个 baseUri - 但是在哪里? Unfortunately, I've found only examples and documentations with Grizzly Test ContainerFactory instead of GrizzlyWebTestC... Could someone explain me what I did miss?不幸的是,我只找到了 Grizzly Test ContainerFactory 而不是 GrizzlyWebTestC 的示例和文档......有人能解释一下我错过了什么吗?

Any input is much appreciated!非常感谢任何输入!

This is my Unit test:这是我的单元测试:

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

This my Endpoint, I would like to test:这是我的端点,我想测试:

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

This is the most important part in my pom.xml这是我 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>

This issue causes because in org.glassfish.jersey.test.JerseyTest class you can find method with @Before annotation related to junit4这个问题是因为在 org.glassfish.jersey.test.JerseyTest class 你可以找到与 junit4 相关的 @Before 注释的方法

@Before
public void setUp() throws Exception {
..
}

When you replace junit4 to junit5 this annotation do nothing and method doesn't invoke.当您将 junit4 替换为 junit5 时,此注释不执行任何操作,并且不会调用方法。

Solution:解决方案:

public class YourJerseyTest extends JerseyTest {

@BeforeEach
void init() throws Exception {
    super.setUp();
}
..
}

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

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