2015-04-01 94 views
3

要求大家幫忙嗎?
jersey2單元測試,HttpServletRequest爲空

球衣錯誤連接:[1]:https://java.net/jira/browse/JERSEY-2412

該servlet請求,響應和上下文時使用測試提供商(測試碼頭和grizzly2)我不注入類。 我使用包註釋來拉起應用程序。


您有其他方法嗎?


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) {} 

請求和響應爲空。

回答

5

您需要爲一個Servlet環境配置JerseyTest。在你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(); 
} 

如果你看看ServletDeploymentContext.forServlet,它返回一個ServletDeploymentContext.Builder。如果你看看Javadoc,你會看到一些熟悉的方法,比如initParam(...,...),addListener等。這就像編寫你的web.xml一樣。只需保持鏈接方法,然後構建。

通過上述配置,您不再需要覆蓋JerseyTest中的configure方法。只需添加如上所示的ResourceConfig即可。

看其他測試實例here

另請參閱相關的:

+0

喜peeskillet 感謝,我的問題解決了, – jack 2015-04-01 09:52:11