Grails UnitTest

Tom*_*máš 0 grails

嗨(这是一个非常愚蠢的问题)

如何在测试中从数据库中获取Domain类?

class PollServiceTests extends GrailsUnitTestCase {
    Integer id = 1

    void testSomething() {
       Teacher teacher1 = Teacher.get(id)
       assert teacher1 != null
    }
}
Run Code Online (Sandbox Code Playgroud)

我总是得到null或

没有方法签名:cz.jak.Teacher.get()适用于参数类型:(java.lang.Integer)值:[1]

非常感谢汤姆

Llo*_*olz 5

Grails单元测试无法访问Grails环境,因此您必须模拟Domain对象.

void testSomething() {
    mockDomain(Teacher)
    def teacher = new Teacher(...)
    assertNotNull teacher.save(insert:true)
    assertNotNull country.countryId
    def found = Teacher.get(country.countryId)
}
Run Code Online (Sandbox Code Playgroud)