2009-07-11 11 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.MockUtils.prepareForConstraintsTests(MockUtils.groovy:540)でgrails.test.MockUtils $ prepareForConstraintsTests.call(不明 出典)で grails.test.GrailsUnitTestCase.mockForConstraintsTests(GrailsUnitTestCase.groovy:111)で で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)at gant.Gant $ _dispatch_closure4.doCall(Gant.gr oovy:324) gant.Gant $ _dispatch_closure6.doCall(Gant.groovy時:334) gant.Gant $ _dispatch_closure6.doCallで(Gant.groovy) gant.Gant.withBuildListeners(Gant.groovy時:344) でgant.Gant.Gant.this $ 2 $ withBuildListeners(Gant.groovy)に gant.Gant $ this $ 2 $ withBuildListeners.callCurrent(不明なソース)は にあります。gant.Gant.dispatch(Gant.groovy:334)at gant.Gant.this $ 2 $ディスパッチ(Gant.groovy) gant.Gant.invokeMethod(Gant.groovy) gant.Gant.processTargets(Gant.groovy:495)at gant.Gant.processTargets(Gant.groovy:480)によって引き起こされる: java.lang.InstantiationException

わかりません。私はクラスのインスタンスを作成し、それをmockForConstraintsTestsメソッドに渡しました。私は間違って何をしていますか?

答えて

2

これは実際にmockForConstraintsTestsものがグルーヴィーで、一般的にモックのそのタイプを使用してというよりも、問題のGrailsで動作する方法の問題です。

このタイプのモックは、mockForConstraintsTestsによって作成されているモックとは互換性がありません。このライブラリを使いたい場合、Johnはクラスの簡単な具体的なインプリメントを作成して渡すだけです。

実際には実際のものではないので、最近のバージョンの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() 
    } 
}