Here’s my example code
class CommandLine {
def ls() {
def cmd = "ls".execute()
if(cmd.waitFor() != 0) {
throw new Execution()
}
return cmd.text
}
}
The cmd variable holds an object of type java.lang.Process. How would I mock out the waitFor() method in order to test the thrown exception? If I can’t, is there some way this could be rewritten to facilitate automated testing?
In general, how do you mock an object instantiated inside another class, or how do you structure your code to allow for testing?
The answer is to use Groovy mocks instead of the built in Grails mocks.