【发布时间】:2017-07-06 23:44:12
【问题描述】:
当我使用 Jersey 测试框架为具有 @Path 的类运行我的 Maven 测试时,它运行良好。但是当我尝试测试 @WebServlet 类时,它没有,并说这是一个 404 错误。
如何使用 Jersey 测试框架测试 Web servlet? (或者,如果这是不可能的,我还能如何测试呢?)
Web Servlet:
package com.testservlet;
@WebServlet("/test")
public class TestServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.getWriter().println( "The Result" );
}
}
测试类:
import com.testservlet.TestServlet;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Assert;
import org.junit.Test;
import javax.ws.rs.core.Application;
public class TestEndpoint extends JerseyTest
{
@Override
protected Application configure()
{
return new ResourceConfig( JPAServlet.class );
}
@Test
public void baseGetTest()
{
String response = target( "/test" ).request().get( String.class );
Assert.assertTrue( "92096".equals( response ) );
}
}
马文:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>TestWeb</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<!-- Tell Maven what language version to use -->
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- Enables the annotations, etc needed -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<exclusions>
<exclusion>
<groupId>javax.exterprise</groupId>
<artifactId>cdi-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Our jersey libs -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.25.1</version>
</dependency>
<!-- CDI to JAX-RS Binding -->
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers.glassfish/jersey-gf-cdi-ban-custom-hk2-binding -->
<dependency>
<groupId>org.glassfish.jersey.containers.glassfish</groupId>
<artifactId>jersey-gf-cdi</artifactId>
<version>2.14</version>
</dependency>
<!-- TESTING WEB -->
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-jdk-http</artifactId>
<version>2.25.1</version>
</dependency>
</dependencies>
</project>
【问题讨论】:
标签: java maven jakarta-ee jersey jersey-test-framework