I'm beginning with jersey and trying to get freemarker working with it using TDD. I want to make a ViewProcessor
for my templates, but fail to inject the servlet context in the class.
Here is the class code :
@Provider
public class myProcessor implements ViewProcessor<Template> {
[...]
@Context
public ServletContext myContext;
[...]
freemarkerConfiguration.setTemplateLoader(
new WebappTemplateLoader(myContext,
myContext.getInitParameter("freemarker.template.path")));
[...]
}
And here is the test code :
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);
}
}
I use maven with dependencies as follow :
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly</artifactId>
<version>1.5-SNAPSHOT</version>
<scope>test</scope>
</dependency>
My code runs fine when i deploy to my local jetty server. But if i want to test the code in my IDE, it failed to inject the servlet context (@Context
) : myContext
is null
when I run the test :/
I think I'm missing something, but I'm a complete beginner with servlet world.