Unit testing 第二次在服务测试中使用GrailsApplication模拟失败
我正在对Grails服务进行单元测试,并使用mock模拟对 GrailsApplication类。我有一个测试成功了,但是当我尝试的时候 随后的测试失败。我在用需求来模拟isDomainClass 方法。我试着复制和粘贴测试中的代码 成功执行失败的测试方法,但第二次使用相同的代码 运行它失败,表示不需要再调用isDomainClass。我是 我怀疑两种方法之间有泄漏,但我看不出它在哪里 我已经尝试过的事情:Unit testing 第二次在服务测试中使用GrailsApplication模拟失败,unit-testing,grails,groovy,mocking,Unit Testing,Grails,Groovy,Mocking,我正在对Grails服务进行单元测试,并使用mock模拟对 GrailsApplication类。我有一个测试成功了,但是当我尝试的时候 随后的测试失败。我在用需求来模拟isDomainClass 方法。我试着复制和粘贴测试中的代码 成功执行失败的测试方法,但第二次使用相同的代码 运行它失败,表示不需要再调用isDomainClass。我是 我怀疑两种方法之间有泄漏,但我看不出它在哪里 我已经尝试过的事情: 从命令行运行测试(我在SpringSource工具套件版本2.7.0.20110529
- 从命令行运行测试(我在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)
在多个测试用例中尝试在jms消息接口上使用mockFor时,我遇到了类似的错误 我通过创建一个自定义接口来解决这个问题,该接口从需要模拟的接口扩展而来。您将使用自定义接口创建模拟 e、 g
我不太清楚为什么,但我认为mockFor在接口和非接口之间的行为有所不同。但这只是一个猜测。您运行的是什么版本的Grails??
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..