java - How to access a file in the .war classpath during unit tests? -
i have web application project, , i'm trying unit test method creates file using freemarker template. method createfile() should take myfile type - contains file name create , rootmap freemarker needs , template name - , create file using template provide.
i following freemarker manual set template loader. problem is, i'm using templateloader setclassfortemplateloading(class, string) method find template path. template loader uses class.getresource() classpath.
but, since i'm using maven, have java code in /src/main/java, template in /src/main/webapp/templates/ , test code in /src/test/java. therefore, class.getresource("/") (root classpath) returns <path_to_project>/target/test-classes/
.
since deploying war, cannot use setdirectoryfortemplateloading(file). also, since i'm testing app don't have servletcontext use setservletcontextfortemplateloading(object, string).
how can access template folder test case?
here's simplified example of test code (i use mockito mock behaviour of myfile class):
private myfile myfile; private filegenerator filegenerator; @before public void setup() { myfile = new myfile(...); filegenerator = new filegenerator(myfile, ...); } @test public void shouldcreatefile() { final myfile mockedmyfile = spy(file); final map<string, object> rootmap = new hashmap<string, object>(); // populates rootmap stuff needed template // mocking method return when(mockedmyfile.getrootmap()).thenreturn(rootmap); // replacing myfile implementation mock filegenerator.setmyfile(mockedmyfile); // calling method want test filegenerator.createfile(); asserttrue(myfile.getfile().exists()); }
and here simplification of code i'm testing:
public void createfile() { final configuration cfg = new configuration(); cfg.setclassfortemplateloading(getclass(), "templates/"); try { myfile.getfile().createnewfile(); final template template = cfg.gettemplate("template.ftl"); final writer writer = new filewriter(myfile.getfile()); template.process(myfile.getrootmap(), writer); writer.flush(); writer.close(); } // exception handling }
i applied charles forsythe's suggestion, worked out fine.
i added templateloader member filegenerator class, own getter , setter.
next, in createfile method, use method settemplateloader(templateloader) configuration class, such:
public void createfile() { final configuration cfg = new configuration(); // changed cfg.settemplateloader(templateloader); // rest }
finally, create template loader test:
@test public void shouldcreatefile() { final myfile mockedmyfile = spy(file); final map<string, object> rootmap = new hashmap<string, object>(); templateloader templateloader = null; try { templateloader = new filetemplateloader(new file("<path template folder>")); } catch (final ioexception e) { e.printstacktrace(); } gerador.settemplateloader(templateloader); // rest }
and problem solved. in production code, use classtemplateloader instead of filetemplateloader.
Comments
Post a Comment