2011-06-16 82 views
0

我正在測試一個Grails服務並使用Mocks來模擬調用到GrailsApplication類的 。我有一個測試成功,但當我嘗試後續測試失敗時。我正在使用需求來模擬isDomainClass 方法。我試圖複製和粘貼代碼從測試 成功的測試方法失敗,但第二次相同的代碼 運行失敗,說沒有更多的調用isDomainClass預計。我是 懷疑方法之間的一些泄漏,但我不知道它在哪裏。GrailsApplication Mock在服務測試失敗中的第​​二次使用

事情我已經試過不已:

  • 運行命令行(我跑下SpringSource工具套件版本2.7.0.201105292341-M2的測試。)
  • 測試移動測試失敗到一個不同的測試類(運行第一成功測試)
  • 改變需求子句中的號碼範圍,以1..5(第二測試仍然失敗)

下面是相關部

package simulation 

import grails.test.* 
import org.joda.time.* 
import org.codehaus.groovy.grails.commons.GrailsApplication 

class ObjectSerializationServiceTests extends GrailsUnitTestCase { 

     def objectSerializationService 

    protected void setUp() { 
     super.setUp() 
       objectSerializationService = new ObjectSerializationService() 
    } 

    protected void tearDown() { 
     super.tearDown() 
       objectSerializationService = null 
    } 

     void testDomainObjectSerialization() { 
       def otherControl = mockFor(GrailsApplication) 
       otherControl.demand.isDomainClass(1..1) {true} 
       otherControl.demand.getDomainClass(1..1) {className -> 
         assert className == "simulation.TestDomainClass" 
         TestDomainClass.class 
       } 
       objectSerializationService.grailsApplication = otherControl.createMock() 

       def now = new DateTime() 
       def testObject = new TestDomainClass([id:57, someOtherData:"Some Other 
Data", theTime:now]) 
       def testInstances = [testObject] 
       mockDomain(TestDomainClass, testInstances) 

       def serialized = objectSerializationService.serializeObject(testObject) 
       def deserialized = 
objectSerializationService.deserializeObject(serialized) 

       assert deserialized == testObject 
       assert serialized.objectType == SerializedObject.ObjectType.DOMAIN 

       otherControl.verify() 
     } 

    void testSerializableSerialization() { 
       def otherControl = mockFor(GrailsApplication) 
       otherControl.demand.isDomainClass(1..1) {true} 
       otherControl.demand.getDomainClass(1..1) {className -> 
         assert className == "simulation.TestDomainClass" 
         TestDomainClass.class 
       } 
       objectSerializationService.grailsApplication = otherControl.createMock() 

       def now = new DateTime() 
       def testObject = new TestDomainClass([id:57, someOtherData:"Some Other 
Data", theTime:now]) 
       def testInstances = [testObject] 
       mockDomain(TestDomainClass, testInstances) 

       def serialized = objectSerializationService.serializeObject(testObject) 
       def deserialized = 
objectSerializationService.deserializeObject(serialized) 

       assert deserialized == testObject 
       assert serialized.objectType == SerializedObject.ObjectType.DOMAIN 

       otherControl.verify() 
    } 

} 

和輸出:我的測試案例

Testcase: testDomainObjectSerialization took 0.943 sec 
Testcase: testSerializableSerialization took 0.072 sec 
     FAILED 
junit.framework.AssertionFailedError: No more calls to 'isDomainClass' 
expected at this point. End of demands. 
     at grails.test.MockClosureProxy.doBeforeCall(MockClosureProxy.java:66) 
     at grails.test.AbstractClosureProxy.call(AbstractClosureProxy.java:74) 
     at 
simulation.ObjectSerializationService.serializeObject(ObjectSerializationService.groovy:20) 
     at simulation.ObjectSerializationService$serializeObject.call(Unknown 
Source) 
     at 
simulation.ObjectSerializationServiceTests.testSerializableSerialization(ObjectSerializationServiceTests.groovy:68) 
+0

您運行的是哪個版本的Grails? – gotomanners 2011-06-16 10:35:48

回答

1

我有一個類似的錯誤嘗試使用mockFor在多個測試用例JMS消息接口。

我通過創建一個自定義接口來擴展接口,從而需要嘲笑它。您將使用自定義界面來創建模擬。

例如

private interface GrailsApplicationTest1 extends GrailsApplication(){} 
testOne(){ 
    def control = mockFor(GrailsApplicationTest1) 
    //...rest of code 
} 

private interface GrailsApplicationTest2 extends GrailsApplication(){} 
testTwo(){ 
    def control = mockFor(GrailsApplicationTest2) 
    //...rest of code 
} 
//add more private interfaces for additional test cases.. 

我不完全確定爲什麼,但我認爲mockFor在接口和非接口之間的行爲有所不同。但這只是一個瘋狂的猜測。

相關問題