I've got a sample Domain such as this
class User {
String username
String password
def userHelper
static contraints = {
username(nullable: false, blank: false)
password nullable: false, blank: false, validator: {pwd, userInstance ->
return userInstance.userHelper.validatePassword(pwd)
}
}
}
the userHelper
is being injected by the following in my resources.groovy
beans = {
userHelper(UserHelper)
}
When I test my application from the browser, everything runs fine. However, I get an exception while trying to write functional tests for this.
The error says: Cannot invoke method validatePassword() on null object
So I'm assuming that userHelper
is not being set when I run my functional test case.
My test case looks like this:
@TestFor(UserController)
@Mock([User])
class UserControllerSpecification extends Specification {
def "save user" () {
given:
request.contentType = "application/json"
request.JSON = """
{user:
{
username: "somtething"
password: "something"
}
}
"""
when:
controller.save()
then:
User.count() == 1
}
}
Update
Controller:
class UserController {
def userService
def save() {
def user = new User(params?.user)
request.withFormat {
json {
if(user.validate())
userService.processUser()
//do something
else
//do something else
}
}
}
}
Questions
- How can I set the
userHelper
property onUser
domain prior to running my tests? - How can I run my code in
Bootstrap.groovy
prior to running all my functional and integration test cases?