测试 – 如何模拟’新’运算符
发布时间:2020-12-14 16:32:49 所属栏目:大数据 来源:网络整理
导读:我正在测试一些使用 java库的groovy代码,我想模拟库调用,因为他们使用网络.所以测试中的代码看起来像: def verifyInformation(String information) { def request = new OusideLibraryRequest().compose(information) new OutsideLibraryClient().verify(re
我正在测试一些使用
java库的groovy代码,我想模拟库调用,因为他们使用网络.所以测试中的代码看起来像:
def verifyInformation(String information) { def request = new OusideLibraryRequest().compose(information) new OutsideLibraryClient().verify(request) } 我尝试使用MockFor和StubFor但我得到的错误如: No signature of method: com.myproject.OutsideLibraryTests.MockFor() is applicable for argument types: (java.lang.Class) values: [class com.otherCompany.OusideLibraryRequest] 我正在使用Grails 2.0.3. 解决方法MockFor ‘s constructor的第二个可选参数是interceptConstruction.如果将此属性设置为true,则可以模拟构造函数.例:
import groovy.mock.interceptor.MockFor class SomeClass { def prop SomeClass() { prop = "real" } } def mock = new MockFor(SomeClass,true) mock.demand.with { SomeClass() { new Expando([prop: "fake"]) } } mock.use { def mockedSomeClass = new SomeClass() assert mockedSomeClass.prop == "fake" } 但是请注意,你只能模拟像这样的groovy对象.如果您坚持使用Java库,则可以将Java对象的构造转换为工厂方法并对其进行模拟. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |