繁体   English   中英

服务测试中第二次使用GrailsApplication模拟失败

[英]Second Use of GrailsApplication Mock in Service Test Fails

我正在对Grails服务进行单元测试,并使用Mocks模拟对GrailsApplication类的调用。 我有一项测试成功,但是当我尝试后续测试时,它们失败了。 我正在使用需求模拟isDomainClass方法。 我尝试将代码从成功的测试复制并粘贴到失败的测试方法,但是第二次运行相同的代码失败,表明不再需要对isDomainClass的调用。 我怀疑这些方法之间存在一些泄漏,但是我看不到它在哪里。

我已经尝试过的事情:

  • 从命令行运行测试(我正在SpringSource Tool Suite版本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)

我在多个测试案例中尝试在jms消息接口上使用mockFor时遇到类似的错误。

我通过创建一个自定义接口扩展了它,该接口从需要模拟的接口扩展而来。 您将使用自定义界面来创建模拟。

例如

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..

我不确定为什么,但是我认为模拟接口在接口和非接口之间的行为有所不同。 但这只是一个疯狂的猜测。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM