小编Bal*_*ick的帖子

使用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
查看次数

标签 统计

freemarker ×1

java ×1

jersey ×1

servlets ×1

testing ×1