i starting program code generator netbeans 8, , having trouble figuring out best way test invoke() method.
the code generator want test this:
(imports here) public class mycodegenerator implements codegenerator { private final jtextcomponent textcomponent; private final compilationcontroller controller; mycodegenerator(final lookup context) { textcomponent = context.lookup(jtextcomponent.class); controller = context.lookup(compilationcontroller.class); } @override public string getdisplayname() { return "generate code..."; } /** * invoked when user chooses generator insert code * dialog */ @override public void invoke() { if (textcomponent != null && controller != null) { controller.tophase(phase.resolved); //do more things source code; } } } i want use mocked (mockito) object lookup, pass mycodegenerator's constructor. mock should return jtextcomponent , compilationcontroller.
i know can provide jtextcomponent test code, hit wall when need provide compilationcontroller.
i can create temporary java source file same content jtextcomponent, not find way create compilationcontroller (or workingcopy) it.
this tried far (my test method):
@test public void testinvoke() throws parseexception, ioexception { system.out.println("invoke"); final extractcontrollertask extracttask = new extractcontrollertask( phase.resolved); final stringbuilder builder = new stringbuilder(100); final jtextcomponent textcomponent; final document document; final fileobject javatestfile; final outputstream outputstream; final javasource source; builder.append("public class clazz {"); builder.append("private int = 2;"); builder.append("}"); textcomponent = new jtextarea(builder.tostring()); document = textcomponent.getdocument(); document.putproperty(basedocument.mime_type_prop, "text/x-java"); javatestfile = fileutil.createdata(new file( "/tmp/javatestsourcefile.java")); outputstream = javatestfile.getoutputstream(); outputstream.write(builder.tostring().getbytes()); outputstream.flush(); source = javasource.forfileobject(javatestfile); assertnotnull(source); source.runuseractiontask(extracttask, true); assertnotnull(extracttask.controller); //fails here } this code extractcontrollertask:
private static class extractcontrollertask implements task<compilationcontroller> { private final phase targetphase; private compilationcontroller controller; private extractcontrollertask(final phase phase) { this.targetphase = phase; } public void run(final compilationcontroller compcontroler) { try { compcontroler.tophase(this.targetphase); this.controller = compcontroler; } catch (ioexception ioe) { throw new runtimeexception(ioe); } } } what surprises me run method in extractcontrollertask never called.
i need test code can't find proper way. maybe approach wrong.
can suggest how achieve this?
Comments
Post a Comment