2010-05-04 5 views
0

안녕하세요 (어리석은 질문입니다)Grails UnitTest

어떻게 테스트에서 데이터베이스에서 도메인 클래스를 얻을 수 있습니까?

class PollServiceTests extends GrailsUnitTestCase { 
    Integer id = 1 

    void testSomething() { 
     Teacher teacher1 = Teacher.get(id) 
     assert teacher1 != null 
    } 
} 

난 항상 널 얻거나

방법의 어떠한 서명 : (java.lang.Integer의) 값 : cz.jak.Teacher.get은() 인수 유형에 적용 할 수 없습니다 : [1]

덕분에 많은 톰

+0

, 내가 == 테스트가 teacher1로 실패합니다 – Armand

답변

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) 
} 
+0

로이드 :-(널 기대, 테스트 데이터베이스에 액세스 유닛 테스트 Grails는 정적 도메인 클래스를 통해 데이터베이스의 데이터를 검색 할 수 (자바 junit 테스트와 같은) 전통적인 unittest처럼 메소드 (리스트, 카운트 등). – ricky

0

목록을 취하는 mockDomain에 대한 선택적인 두 번째 매개 변수가 있습니다. 목록을 전달하면 List에있는 객체 (있는 경우)에 대해 get, list, save, update, delete 메소드가 작동합니다. 당신이 mockDomain (Teacker)를 추가 한 후

void testSomething() { 
    def teachers = [] 
    for (i in 0..9) { 
     teachers.add(new Teacher(name: "teacher${it}")) 
    } 
    mockDomain Teacher, data 

    Teacher t2 = Teacher.get(2) 
    assertEquals "teacher2", t2.name 
}