I've been reading different opinions about how to unittest GUI in Java and it is clear that the best approach is to design the code in a MVC model, in order to be able to test everything. Anyway I would like to ask for your opinion on a more concrete problem:
I have a GUI in Java. A button has a selection listener that executes some code
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
//code
}
})
My solution for this implementation would be as follows: I place the //code in a method of an external class that will be the "controller" of my model. This method has to be public if I want to call it and that will allow me to test it.
Everything looks pretty easy but I have a couple of questions:
What if the //code is just doing something for the class (viewer) itself? In that case it makes no sense to extract this code to other class or it will indicate that my code smells. Furthermore if the method remains in the viewer it will be private so I'll have no way to test it
What if the //code is getting access to attributes or widgets or whatever within the viewer? In such a case it would make no sense to place the code into another class just for testing reasons due to the fact that this attributes will need to be passed as parameters to this other class and that will make both classes to depend upon eachother
If I still move the //code to the new class: should I test the behaviour of the button? In Java it is possible to simulate a click on the button. Doing that I would be able to test whether the button is really doing what it is meant to do or not. Is it worth it or should I do it with functional testing?