How do i verify string content using Mockito in Java -
i new using mockito test framework. need unit test 1 method return the string content. same contents stored in 1 .js file (i.e. "8.js"). how verify the string contents returned method expected want.
please find below code generating .js file:
public string generatejavascriptcontents(project project) { try { // creating projectid.js file fileutils.mkdir(outputdir); fileoutputstream = new fileoutputstream(outputdir + project.getid() + ".js"); streamwriter = new outputstreamwriter(fileoutputstream, "utf-8"); stringtemplategroup templategroup = new stringtemplategroup("vitemplates", "/var/vi-xml/template/", defaulttemplatelexer.class); stringtemplate = templategroup.getinstanceof("standardjstemplate"); stringtemplate.setattribute("projectidval", project.getid()); stringtemplate.setattribute("widthval", project.getdimension().getwidth()); stringtemplate.setattribute("heightval", project.getdimension().getheight()); stringtemplate.setattribute("playerversionval", project.getplayertype().getid()); stringtemplate.setattribute("finaltagpath", finalpathbuilder.tostring()); streamwriter.append(stringtemplate.tostring()); return stringtemplate.tostring(); } catch (exception e) { logger.error("exception occurred while generating standard tag type content", e); return ""; } }
the output of above method writes .js file , contents of file looks below:
var projectid = 8;
var playerwidth = 300;
var playerheight = 250;
var player_version = 1;
.....
i have written testmethod()
using mockito test this, able write .js file using test method, how verify contents?
can me sort out problem?
as @Ćukaszbachman mentions, can read contents js file. there couple of things consider when using approach:
- the test slow, have wait js content written disk, read content disk , assert content.
- the test theoretically flaky because entire js content may not written disk time code reads file. (on note, should consider calling flush() , close() on outputstreamwriter, if aren't already.)
another approach mock outputstreamwriter
, inject method. allow write test code similar following:
outputstreamwriter mockstreamwriter = mock(outputstreamwriter.class); generatejavascriptcontents(mockstreamwriter, project); verify(mockstreamwriter).append("var projectid = 8;\nvar playerwidth = 300;...");
http://mockito.googlecode.com/svn/branches/1.5/javadoc/org/mockito/mockito.html#verify%28t%29
Comments
Post a Comment