jersey2 Unit testing,HttpServletRequest is null(jersey2 单元测试,HttpServletRequest 为空)
问题描述
请大家帮忙?
球衣错误连接:[1]:https://java.net/jira/browse/泽西-2412
jersey Bug connection: [1]: https://java.net/jira/browse/JERSEY-2412
当我使用测试提供者(测试过的码头和 grizzly2)时,servlet 请求、响应和上下文没有注入到类中.我使用包注释来拉起应用程序.
The servlet request, response and context not injected into the class when I using test provider (tested jetty and grizzly2). I using packages annotation to pull up the application.
你还有什么办法吗?
Do you have any other way?
public class VMResourceTest extends BaseTest {
@Test
public void testCreateVm() {
String bodyData = loadClassPathData(CLASS_PATH+File.separator+"tools"+File.separator+"createVm.json");
Response response = target("/tool/cloud/vrm/fm/ghca_vms").queryParam("platform_id", "A22A4B0C3AEC49F5916EA8CC01F56E9A")
.request().header("X-Auth-GHCA-User-ID", "X-Auth-GHCA-User-ID")
.post(Entity.entity(bodyData, MediaType.APPLICATION_JSON));
assertEquals("200", response.getStatus());
}
}
<小时>
public class BaseTest extends JerseyTest{
public String CLASS_PATH = "classpath:";
public WebTarget target;
public Client client;
@Override
protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
ResourceConfig rc = new ResourceConfig().packages("com.ghca.easyview.server.api.resource");
rc.register(SpringLifecycleListener.class);
rc.register(RequestContextListener.class);
rc.property("contextConfigLocation", "classpath:spring/spring-config.xml");
return rc;
}
public String loadClassPathData(String classFilePath){
File schemaContextFile = null;
String result = "";
try {
schemaContextFile = readSchemaFile(classFilePath);
BufferedReader br = new BufferedReader(new FileReader(schemaContextFile));
String s = null;
while((s = br.readLine())!=null){
result = result + "
" +s;
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
<小时>
@Component
@Path("tool/cloud/vrm")
public class VMResource extends BaseResource{
@Autowired
private VMService vmService;
@Context
public HttpServletRequest request;
@Context
public HttpServletResponse response;
@POST
@Path("{platform_type}/ghca_vms")
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public Response createVm(@PathParam("platform_type") String platformType,
@QueryParam("platform_id") String platformId) {}
请求和响应为空.
推荐答案
您需要为 Servlet 环境配置 JerseyTest.在你的 JerseyTest 中,你应该有类似
You need to configure the JerseyTest for a Servlet environment. In your JerseyTest, you should have something like
@Override
protected TestContainerFactory getTestContainerFactory() {
return new GrizzlyWebTestContainerFactory();
}
@Override
protected DeploymentContext configureDeployment() {
ResourceConfig config = new ResourceConfig(SessionResource.class);
return ServletDeploymentContext.forServlet(
new ServletContainer(config)).build();
}
如果您查看 ServletDeploymentContext.forServlet,它会返回 ServletDeploymentContext.Builder.如果您查看 Javadoc,您会看到一些看起来很熟悉的方法,例如 initParam(...,...)、addListener 等.这就像构建你的 web.xml 以编程方式.只需保持链接方法,然后构建.
If you look at the ServletDeploymentContext.forServlet, it returns a ServletDeploymentContext.Builder. If you look at the Javadoc, you will see some familiar looking methods, like initParam(...,...), addListener, etc. This is just like building your web.xml programmatically. Just keep chaining methods, then build.
通过上述配置,您不再需要重写JerseyTest中的configure方法.只需添加如上所示的 ResourceConfig.
With the above configuration, you no longer need to override the configure method in the JerseyTest. Just add the ResourceConfig like seen above.
查看其他测试示例这里
另见相关:
- 如何对 Spring-Jersey 进行内存单元测试
这篇关于jersey2 单元测试,HttpServletRequest 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:jersey2 单元测试,HttpServletRequest 为空
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
