Straight from page 104 of "The Definitive Guide to Grails (Second Edition)":
void testLoginUserNotFound() {
mockRequest.method = "POST"
mockDomain(User)
MockUtils.prepareForConstraintsTests(LoginCommand)
def cmd = new LoginCommand(login:"fred", password:"letmein")
cmd.validate()
controller.login(cmd)
assertTrue cmd.hasErrors()
assertEquals "user.not.found", cmd.errors.login
assertEquals "/store/index", renderArgs.view
}
When this test is run, it fails with:
junit.framework.AssertionFailedError: junit.framework.AssertionFailedError: null
...which I tracked down to the "cmd" reference being null at that point. Before the action controller.login is called, cmd is valid and filled, afterwards, it is null.
How can I test command objects?