Grails: test domain object constraints
I'm trying to test the constraints of my User
domain class
class UserTests extends GrailsUnitTestCase {
protected void setUp() {
super.setUp()
mockForConstraintsTests(User)
}
void testEmailConstraint() {
// Test e-mail validation
def user = new User(userRealName: 'bob', passwd: 'foo')
// Check null not allowed
saveAndVerifyError user, 'email', 'nullable'
}
private void saveAndVerifyError(User user, String field, String constraintName) {
assertFalse user.validate()
assertEquals constraintName, user.errors[field]
}
}
I got the information about how to do this on this webpage, but when I run this test I get the following exception
java.lang.NullPointerException
at grails.test.MockUtils$_addValidateMethod_closure83.doCall(MockUtils.groovy:973)
at grails.test.MockUtils$_addValidateMethod_closure84.doCall(MockUtils.groovy:1014)
at com.mycompany.securi开发者_如何学Goty.UserTests.saveAndVerifyError(UserTests.groovy:31)
at com.mycompany.security.UserTests.this$6$saveAndVerifyError(UserTests.groovy)
at com.mycompany.security.UserTests$this$6$saveAndVerifyError.callCurrent(Unknown Source)
at com.mycompany.security.UserTests.testEmailConstraint(UserTests.groovy:18)
It turns out this is a bug in Grails 1.3.3. The following changes workaround this problem
protected void setUp() {
super.setUp()
PluginManagerHolder.pluginManager = [hasGrailsPlugin: {String name -> true }] as GrailsPluginManager
mockForConstraintsTests(User)
}
protected void tearDown() {
super.tearDown()
PluginManagerHolder.pluginManager = null
}
精彩评论