gpt4 book ai didi

java EasyMock 忽略从 TestedClass 的方法对对象的调用

转载 作者:行者123 更新时间:2023-11-30 05:05:10 25 4
gpt4 key购买 nike

我有一个类,其中有一个对象。我正在测试一个调用该对象的方法,但该对象与我的测试无关,因此我想忽略它。

Class TestedClass {
ObjectX obj;

method() {
/* some processing */
obj.someMethod().otherMethod(lotofparameters); /* i want to ignore this line in my test */
}
}

所以我想测试 method() 而不用参数调用 obj 上的这些方法。

谢谢你

最佳答案

使用createMock方法模拟它。然后使用 anyTimes 方法基本上忽略对它的任何调用。

expect(objMock.someMethod())
.andReturn(42).times(3)

我附上了一份返回单,以防万一您需要它做某事。查看their documentation了解更多。

编辑(解决第一条评论)

使someMethod返回另一个模拟。然后模拟 otherObj.otherMethod。至于参数,请考虑使用我链接到的文档中列出的匹配器。您甚至可以制作自己的匹配器,所以也许这可以帮助解决您的问题。

关于java EasyMock 忽略从 TestedClass 的方法对对象的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5353448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com