2011-06-16 80 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在接口和非接口之间的行为有所不同。但这只是一个疯狂的猜测。