2010-07-10 3 views
1

Je suis en train de tester les contraintes de ma classe User de domaineGrails: essai domaine contraintes d'objet

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] 
    } 
} 

Je suis les informations sur la façon de le faire sur this webpage, mais quand je lance ce test j'obtenir la exception suivante

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.security.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) 

Répondre

3

Il s'avère que c'est a bug in Grails 1.3.3. Les modifications suivantes ont contourné ce problème.

protected void setUp() { 
    super.setUp() 
    PluginManagerHolder.pluginManager = [hasGrailsPlugin: {String name -> true }] as GrailsPluginManager 
    mockForConstraintsTests(User) 
} 

protected void tearDown() { 
    super.tearDown() 
    PluginManagerHolder.pluginManager = null 
} 
Questions connexes