Unit testing 使用Grails对域类进行单元测试
目前正在搜索教程、说明和示例。 我尝试了不同的例子,得出了不同的错误。 我当前的错误是: |编译错误编译[unit]测试时出错:启动失败: 在我的测试报告中。它的输出是: 单元测试结果-总结 未执行任何测试。 我的“UserSpec.groovy”代码如下:Unit testing 使用Grails对域类进行单元测试,unit-testing,grails,groovy,Unit Testing,Grails,Groovy,目前正在搜索教程、说明和示例。 我尝试了不同的例子,得出了不同的错误。 我当前的错误是: |编译错误编译[unit]测试时出错:启动失败: 在我的测试报告中。它的输出是: 单元测试结果-总结 未执行任何测试。 我的“UserSpec.groovy”代码如下: package newmyproject245 import grails.test.mixin.* import spock.lang.Specification @TestFor(User) class UserSpec exten
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"
}
}
有人能帮忙吗。我是圣杯新手。
谢谢
除了上述问题之外,
当我在课堂上省略了约束时,如图所示:
class UserSpec extends Specification {
我发现了这个错误:
|正在运行1单元测试。。。第1页,共1页
|失败:初始化错误(org.junit.runner.manipulation.Filter)
|异常:未找到与grails测试目标匹配的测试
来自org.junit.runner.Request的模式过滤器$1@12c27788
位于org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:35)
位于org.junit.runner.JUnitCore.run(JUnitCore.java:138)
|完成1个单元测试,1个在0米0秒内失败
|错误运行测试时发生致命错误:Not null属性引用临时值-在当前操作之前必须保存临时实例:newmyproject245.Order.product->newmyproject245.product;嵌套异常为org.hibernate.TransientPropertyValueException:Not null属性引用一个临时值-在当前操作之前必须保存临时实例:newmyproject245.Order.product->newmyproject245.product(使用--stacktrace查看完整跟踪)
有人来帮忙。再次感谢 我已经得到了答案。参考代码: 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
}
}
并确保测试环境的dbCreate为“create drop”,以避免此类错误。在DataSource.groovy中找到
问候,
谢谢!我已经得到了答案。参考代码: 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
}
}
并确保测试环境的dbCreate为“create drop”,以避免此类错误。在DataSource.groovy中找到
问候,
谢谢!(^uuz~)