我现在是使用的jersey2.1.3 实现的restful API,现在是编写juint结合jersey 本省的jerseyTest 来编写单元测试,
现在遇到一个问题时,通过jersey测试时,Resource.class 的
@Context
public HttpServletRequest request;
@Context
public HttpServletResponse response;
这两个都是null、正常启动服务时调用是正常的。
点击打开链接这个是jersey 发布的bug 说明,暂时还没有解决版本,
有谁遇到过吗 或者怎么解决的,谢谢了?
以下是我分享一部分代码
package com.ghca.easyview.server.api.resource.test;
import static org.junit.Assert.assertEquals;
import java.io.File;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.junit.Test;
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());
}
}
这个是测试类,集成了BaseTest
package com.ghca.easyview.server.api.resource.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spring.SpringLifecycleListener;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
import org.springframework.util.ResourceUtils;
import org.springframework.web.context.request.RequestContextListener;
public class BaseTest extends JerseyTest{
// public final String BASE_URL = "http://localhost:9998";
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));//构造一个BufferedReader类来读取文件
String s = null;
while((s = br.readLine())!=null){//使用readLine方法,一次读一行
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;
/**
* @param @param platformType
* @param @param platformId
* @param @return
* @return Response
* @throws
*/
@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 和 response都是 null,
哪位大神遇到类似的问题呢,谢谢了