EasyMock 和修改可变方法参数
如何使用 EasyMock 修改模拟方法的可变方法参数?
例如,我有一个使用 BlockingQueue 的类。我想模拟 BlockingQueue 成员进行单元测试。我的类调用方法queue.drainTo(Collection c)。调用此方法将从队列中删除元素并将它们添加到集合中。我如何使用 EasyMock 模拟这种行为?例子会很棒。
How does one use EasyMock to modify a mocked method's mutable method parameter?
For example, I have class that uses a BlockingQueue. I want to mock the BlockingQueue member for unit testing. My class calls the method queue.drainTo(Collection c). Calling this method removes elements from the queue and adds them to the collection. How would I mock this behavior using EasyMock? Examples would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 andAnswer 和 getCurrentArguments:
有时有助于提取辅助类或方法:
那么您可以这样做:
最好使用真正的 BlockingQueue 并找到一种方法,在您希望删除的方法之前将所需的值插入队列中来自队列的数据。
You can use andAnswer and getCurrentArguments:
It sometimes helps to extract a helper class or method:
Then you can do:
It might be better to use a real
BlockingQueue
and find a way to insert the desired value into the queue before the method that you expect to remove data from the queue.很难确切地说出你的代码是什么样的。如果我知道您想要测试的代码,可以更好地帮助您..但是假设您想要测试的代码如下所示:
测试会很
如果这不正确, 抱歉,但很难建议对我想要测试的代码进行测试看不到...
编辑 - 我们不能真正断言可变参数会因为我们使用模拟的方式而改变。我们所能做的就是断言drainTo方法被调用。如果drainTo做了我们想做的事情,则必须在其他地方进行测试..即在BlockingQueue.class
编辑2的测试中-我们可以更具体地了解我们期望调用该方法的列表。
hard to tell exactly what your code looks like. Could help you better if I knew the code you want to test.. but assuming your code you want to test looks like this:
A test would be
Sorry if that isn't right, but really hard to suggest a test for code that I can't see...
Edit - we can't really assert that the mutable param gets changed becuase of how we are using the mock. All we can do is assert that the drainTo method gets called. If drainTo does what we want to do would have to be tested elsewhere.. i.e. in the tests of BlockingQueue.class
Edit 2 - we can be more specific about what list we expect the method to get called with.