简体   繁体   English

jersey2单元测试,HttpServletRequest为空

[英]jersey2 Unit testing,HttpServletRequest is null

Ask everybody to help? 请大家帮忙吗?

jersey Bug connection: [1]: https://java.net/jira/browse/JERSEY-2412 球衣错误连接:[1]: https//java.net/jira/browse/JERSEY-2412

The servlet request, response and context not injected into the class when I using test provider (tested jetty and grizzly2). 当我使用测试提供程序(已测试jetty和grizzly2)时,未将servlet请求,响应和上下文注入到类中。 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 + "\n" +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) {}  

request and response is null. request和response为null。

You need to configure the JerseyTest for a Servlet environment. 您需要为Servlet环境配置JerseyTest In your JerseyTest , you should have something like JerseyTest ,您应该有类似

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

@Override
protected DeploymentContext configureDeployment() {
    ResourceConfig config = new ResourceConfig(SessionResource.class);
    return ServletDeploymentContext.forServlet(
                             new ServletContainer(config)).build();
}

If you look at the ServletDeploymentContext.forServlet , it returns a ServletDeploymentContext.Builder . 如果查看ServletDeploymentContext.forServlet ,它将返回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. 如果您查看Javadoc,将会看到一些熟悉的方法,例如initParam(...,...)addListener等。这就像以编程方式构建web.xml一样。 Just keep chaining methods, then build. 只需保留链接方法,然后进行构建即可。

With the above configuration, you no longer need to override the configure method in the JerseyTest . 使用上述配置,您不再需要覆盖JerseyTestconfigure方法。 Just add the ResourceConfig like seen above. 只需像上面看到的那样添加ResourceConfig

See other test examples here 在这里查看其他测试示例

Also See related: 另请参阅相关:

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

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