【问题标题】:Unit testing for domain classes using Grails使用 Grails 对域类进行单元测试
【发布时间】:2017-05-17 06:30:50
【问题描述】:

目前正在搜索教程、解释和示例。 我尝试了不同的示例并提出了不同的错误。 我目前的错误是:

| Error Compilation error compile [unit] tests: startup failed:

在我的测试报告中。它输出这个:

单元测试结果 - 总结 未执行任何测试。

我的“UserSpec.groovy”代码是这样的:

package newmyproject245

import grails.test.mixin.*
import spock.lang.Specification

@TestFor(User)
class UserSpec extends ConstraintSpecification {

    def setup() {
        Expectations.applyTo User
    }

    def cleanup() {
    }

    void testShouldDoNothing() {
        Expectations.applyTo User

        user."password is not blank"
        user."password is not nullable"
        user."name is not blank"
        user."name is not nullable"
    }

    void testEventNameConstraints() {
        Expectations.applyTo User
        def user = new User()

        user."name is not blank"
        user."name is not nullable"
    }
}

谁能帮忙。我是grails的新手。 谢谢!

除了上述问题, 当我省略了类中的 Contraints 时:

class UserSpec extends Specification {

我遇到了这个错误:

|运行 1 个单元测试... 1 of 1 |失败:initializationError(org.junit.runner.manipulation.Filter) | java.lang.Exception:没有找到匹配 grails 测试目标的测试 来自 org.junit.runner.Request$1@12c27788 的模式过滤器 在 org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:35) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:138) | 0m 0s完成1个单元测试,1个失败 |错误致命错误运行测试:非空属性引用瞬态值 - 瞬态实例必须在当前操作之前保存:newmyproject245.Order.product -> newmyproject245.Product;嵌套异常是 org.hibernate.TransientPropertyValueException: Not-null property references a transient value - 在当前操作之前必须保存瞬态实例:newmyproject245.Order.product -> newmyproject245.Product(使用 --stacktrace 查看完整跟踪)

有人帮忙。再次感谢!

【问题讨论】:

    标签: unit-testing grails groovy


    【解决方案1】:

    我已经得到了答案。参考代码:

    UserSpec.groovy

    package project101
    
    import grails.test.mixin.TestMixin
    import grails.test.mixin.support.GrailsUnitTestMixin
    import spock.lang.Specification
    
    /**
     * See the API for {@link grails.test.mixin.support.GrailsUnitTestMixin} for usage instructions
     */
    @TestMixin(GrailsUnitTestMixin)
    @TestFor(User)
    class UserSpec extends Specification {
    
        def user
    
        def setup() {
            user = new User(firstName: 'FIRSTNAME', lastName: 'LASTNAME', address: 'Finland', username: 'user1', password: 'pass123', userType: 'ADMIN')
    
        }
    
        def cleanup() {
            user = null
        }
    
        void "Test if User handles"() {
            given:
                setup()
            when: "User field has null value"
                user?.username = null
            then: "Validation returns false"
                user?.validate() == false
                user?.errors?.hasFieldErrors('username') == true
        }
    }
    

    并确保 test 环境dbCreate 是“create-drop”以避免此类错误。在 DataSource.groovy

    中找到
    test {
            dataSource {
                pooled = true
                dbCreate = "create-drop"
    

    问候,

    谢谢! (^_~)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-14
      • 1970-01-01
      • 2010-11-15
      相关资源
      最近更新 更多