2009-07-11 7 views
1

Unit testing Abstract classes in GroovymockForConstraintsTests 추상적 인 그루비 클래스

내가 단위 테스트 및 도메인 클래스를 조롱에 대한 이전 질문을,하지만 난 충분히 특이 생각하지 않습니다. 가 [toplevel.domain.Party] 클래스의 새 인스턴스를 만들 수 없습니다 : 여기

import grails.test.* 
import toplevel.domain.* 

class PartyTests extends GrailsUnitTestCase { 
    Party party 
    protected void setUp() { 
     super.setUp() 
     party = [:] as Party 
     mockForConstraintsTests(Party, [party]) 
    } 

    protected void tearDown() { 
     super.tearDown() 
    } 

    void testNullRolesIsValid() { 
     party.roles = null 
     assertTrue "The roles should be nullable", party.validate() 
    } 
} 

테스트 결과입니다 :

package toplevel.domain 

abstract class Party { 
    static hasMany = [roles:PartyRole] 
    static constraints = { 
     roles(nullable:true) 
     dateCreated(display:false) 
     lastUpdated(display:false) 
    } 
    List roles 
    Date dateCreated 
    Date lastUpdated 
} 

가 여기 내 단위 테스트입니다 : 나는 도메인 클래스가 있습니다!

org.codehaus.groovy.grails.exceptions.NewInstanceCreationException

: 는 [toplevel.domain.Party] 클래스의 새 인스턴스를 만들 수 없습니다! 에서 : grails.test.GrailsUnitTestCase.mockForConstraintsTests (111 GrailsUnitTestCase.groovy)에서 grails.test.MockUtils $ prepareForConstraintsTests.call (알 소스)에서 : grails.test.MockUtils.prepareForConstraintsTests (540 MockUtils.groovy)에서 PartyTests.setUp (PartyTests.groovy : 9) : $ _GrailsTest_groovy _run_closure2.doCall (_GrailsTest_groovy 147)에서 _GrailsTest_groovy $ _run_closure4.call에서 (_GrailsTest_groovy) _GrailsTest_groovy $ _run_closure1_closure19.doCall에서 (_GrailsTest_groovy _GrailsTest_groovy $ _run_closure4.doCall (203 _GrailsTest_groovy)에서 : 113) 에서 _GrailsTest_groovy $ _run_closure1.doCall (_GrailsTest_groovy : 96)에서 TestApp $ _run_closure1.doCall (TestApp.groovy : 66) 에서 gant.Gant $ _dispatch_closure4.doCall (Gant.gr oovy : 에서 344) : gant.Gant.withBuildListeners (Gant.groovy에서 gant.Gant $ _dispatch_closure6.doCall (Gant.groovy)에서 334) : gant.Gant $ _dispatch_closure6.doCall (Gant.groovy에서 324) gant.Gant.this $ 2 $ withBuildListeners (Gant.groovy) gant.Gant $ this $ 2 $ withBuildListeners.callCurrent (알 수없는 소스) gant.Gant.dispatch (Gant.groovy : 334) at gant.Gant.this $ 2 $ dispatch (Gant.groovy) at gant.invokeMethod (Gant.groovy) at gant.Gant.processTargets (Gant.groovy : 495) at gant.Gant.processTargets (Gant.groovy : 480)에 의해 발생했습니다. : java.lang.InstantiationException

이해가 안됩니다. 클래스의 인스턴스를 만들어 mockForConstraintsTests 메서드에 제공했습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변

2

이 실제로 mockForConstraintsTests 물건 오히려 당연 일반적으로 모의의 유형을 사용에 문제가보다 Grails가 작동하는 방식에 문제가있다.

이 유형의 모의은 mockForConstraintsTests에 의해 생성되는 모의와 호환되지 않습니다. 이 라이브러리를 사용하고자한다면, John은 클래스의 단순한 구체적인 impl을 생성하고 전달하는 것에 대해 정확합니다.

저는 사실 grails의 최근 버전에있는 "실제"가 아니기 때문에 제약 조건을 조롱하는 사람이 아닙니다. 조롱당한 물건의 상당 부분은 실제 코드가 실행될 때와 다른 경우입니다. 실제 데이터베이스에 연결.이러한 종류의 제약 조건을 테스트하기 위해 통합 테스트를 사용하는 것을 선호합니다. 당신은 통합 테스트에 동일한 테스트 클래스를 넣고 mockForConstraintsTests 전화 제거하면

, 코드가 작동합니다 :

package toplevel.domain 

import grails.test.* 

class PartyTests extends GrailsUnitTestCase { 
    Party party 
    protected void setUp() { 
     super.setUp() 
     party = [:] as Party 
    } 

    protected void tearDown() { 
     super.tearDown() 
    } 

    void testNullRolesIsValid() { 
     party.roles = null 
     assertTrue "The roles should be nullable", party.validate() 
    } 
} 

결과 :

Running 1 integration test... 
Running test PartyTests...PASSED 
Tests Completed in 226ms ... 
------------------------------------------------------- 
Tests passed: 1 
Tests failed: 0 
------------------------------------------------------- 
3

당신은 구체적인 Party 클래스를 제공해야합니다. 테스트는 Party 클래스의 인스턴스를 만들려고 시도하는 것이므로 추상 클래스가 아닙니다. 아래에서 테스트를 다시하고 변경 한 곳에 대해 의견을 말했습니다.

package toplevel.domain 

import grails.test.* 
import toplevel.domain.* 

// Create a stub implementation class 
class PartyImpl extends Party { } 

class PartyTests extends GrailsUnitTestCase { 
    Party party 
    protected void setUp() { 
     super.setUp() 
     //party = [:] as Party 
     // Create an instance of the stub'd class 
     party = new PartyImpl() 
     //mockForConstraintsTests(Party, [party]) 
     // Need to pass in the concrete class as first arg 
     mockForConstraintsTests(PartyImpl, [party]) 
    } 

    protected void tearDown() { 
     super.tearDown() 
    } 

    void testNullRolesIsValid() { 
     party.roles = null 
     assertTrue "The roles should be nullable", party.validate() 
    } 
} 
관련 문제