相关疑难解决方法(0)

使用jersey测试框架时,Servlet上下文注入失败

我开始穿着球衣并尝试使用TDD让freemarker使用它.我想ViewProcessor为我的模板创建一个,但是无法在类中注入servlet上下文.

这是类代码:

@Provider
public class myProcessor implements ViewProcessor<Template> {

    [...]

    @Context
    public ServletContext myContext;

    [...]

 freemarkerConfiguration.setTemplateLoader(
       new WebappTemplateLoader(myContext,
           myContext.getInitParameter("freemarker.template.path")));

    [...]
    }
Run Code Online (Sandbox Code Playgroud)

这是测试代码:

public class myProcessorTest extends JerseyTest {

    public static myProcessor mp;

    public myProcessorTest() throws Exception{
        super(new WebAppDescriptor.Builder("com.domain").build());
    }

    @Test
    public void firstTest(){
        mp = new myProcessor();
        String path = new String("test.ftl");
        Template template = mp.resolve(path);
        assertNotNull(template);
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用maven与依赖关系如下:

<dependency>
    <groupId>com.sun.jersey.jersey-test-framework</groupId>
    <artifactId>jersey-test-framework-grizzly</artifactId>
    <version>1.5-SNAPSHOT</version>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

当我部署到我的本地jetty服务器时,我的代码运行正常.但是,如果我想测试我的IDE代码,它没有注入servlet上下文(@Context):myContextnull,当我运行测试:/

我想我错过了一些东西,但我是servlet世界的初学者.

java testing servlets freemarker jersey

8
推荐指数
1
解决办法
3148
查看次数

jersey2单元测试,HttpServletRequest为null

请大家帮忙?

泽西Bug连接:[1]:https://java.net/jira/browse/JERSEY-2412

当我使用测试提供程序(测试的jetty和grizzly2)时,servlet请求,响应和上下文没有注入到类中.我使用包注释来提取应用程序.


你有其他方法吗?


 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());  
    }  
} 
Run Code Online (Sandbox Code Playgroud)
    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 …
Run Code Online (Sandbox Code Playgroud)

java rest jersey

3
推荐指数
1
解决办法
2782
查看次数

标签 统计

java ×2

jersey ×2

freemarker ×1

rest ×1

servlets ×1

testing ×1