簡體   English   中英

測試問題 Jersey 3 Web 應用程序使用 Jersey 測試框架

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

我有一個 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 - 但是在哪里? 不幸的是,我只找到了 Grizzly Test ContainerFactory 而不是 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>

這個問題是因為在 org.glassfish.jersey.test.JerseyTest class 你可以找到與 junit4 相關的 @Before 注釋的方法

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

當您將 junit4 替換為 junit5 時,此注釋不執行任何操作,並且不會調用方法。

解決方案:

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