tim_yates and kriegaex are the big beasts in the jungle when it comes to good and bad Spock, or TDD-style testing generally ... they have more than once (rightly) picked apart my questions in the way they do here, basically on the basis of testing the code rather than the implementation.
Sometimes it's difficult though. Maybe there can be cases in which you would want to test for the calling of super.doSomething()
. I am just putting together, using TDD, having already done a "spike", in which I rushed ahead without testing, an editor for a TreeTableView
. The "spike" can be seen here. In a constructive comment to my answer, kleopatra advised me to check (i.e. put an if
in the app code) to make sure that super.startEdit()
had indeed started the editing of the cell before going further, so in this case it is not sufficient to test the "side-effect" of super.startEdit()
as being that isEditing()
now returns true
. You genuinely need to know that your class's startEdit()
actually does nothing more nor less than call super.startEdit()
.
However, I don't believe it can be done, and tim_yates or kriegaex would almost certainly have said how you could do that if it were possible.
My suggested TDD solution would therefore be something like this:
def 'super start edit should be called if cell is not empty'(){
given:
// NB has to be GroovySpy because isEmpty() is final
DueDateEditor editor = GroovySpy( DueDateEditor ){
isEmpty() >> false
}
when:
editor.startEdit()
then:
1 * editor.callSuperStartEdit()
}
class DueDateEditor extends TreeTableCell {
@Override
void startEdit(){
if( ! isEmpty() ) {
// this is the line you have to add to make the test pass
callSuperStartEdit()
}
}
def callSuperStartEdit(){
super.startEdit()
}
}
I think you have to "spawn" an artificial single-purpose method since there is, precisely, no side effect at all!
PS I will in fact parameterise this test so that it returns true
to isEmpty()
in the second call, and require the method NOT to be called in that case.