java - Servlet context injection fail while using jersey test framework -
i'm beginning jersey , trying freemarker working using tdd. want make viewprocessor
templates, fail inject servlet context in class.
here 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 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 dependencies 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 deploy local jetty server. if want test code in ide, failed inject servlet context (@context
) : mycontext
null
when run test :/
i think i'm missing something, i'm complete beginner servlet world.
there's couple of ways it. remove constructor , implement configure() method this:
public class myprocessortest extends jerseytest { public static myprocessor mp; @override protected appdescriptor configure() { return new webappdescriptor.builder("com.domain") .contextparam("contextconfiglocation", "classpath:/applicationcontext.xml") .contextpath("/").servletclass(springservlet.class) .contextlistenerclass(contextloaderlistener.class) .requestlistenerclass(requestcontextlistener.class) .build(); }
or alternatively can annotate test spring context:
@runwith(springjunit4classrunner.class) @contextconfiguration("classpath:applicationcontext.xml") public class myprocessortest extends jerseytest { public static myprocessor mp;
Comments
Post a Comment