lundi 20 juillet 2015

How do I test a Java Code Generator in Netbeans?

I am starting to program a Code Generator for Netbeans 8, and I am having trouble figuring out the best way to test its invoke() method.

The code generator I want to test is basically like 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 Some Code..."; 
    } 

    /** 
     * This will be invoked when user chooses this Generator from Insert Code 
     * dialog 
     */ 
    @Override 
    public void invoke() { 
        if (textComponent != null && controller != null) { 
            controller.toPhase(Phase.RESOLVED); 
            //do more things with the source code; 
        }    
    } 
}

I want to use a mocked (Mockito) object for Lookup, to pass to the MyCodeGenerator's constructor. The mock sould return the JTextComponent and the CompilationController.

I know I can provide a JTextComponent with the test code, but I hit the wall when I need to provide a CompilationController.

I can create a temporary java source file with the same content as the JTextComponent, but I could not find a way to create a CompilationController (or WorkingCopy) from it.

I really need to test my code but I can't find the proper way. Maybe the approach is completely wrong...

Can anyone suggest how to achieve this?

Thanks!

Aucun commentaire:

Enregistrer un commentaire