如何告诉 Mockito 模拟对象在下次调用时返回不同的内容?
因此,我在类级别创建一个模拟对象作为静态变量,如下所示...在一个测试中,我希望 Foo.someMethod() 返回某个值,而在另一个测试中,我希望它返回不同的值。我遇到的问题是,我似乎需要重建模拟才能使其正常工作。我想避免重建模拟,而只是在每个测试中使用相同的对象。
class TestClass {
private static Foo mockFoo;
@BeforeClass
public static void setUp() {
mockFoo = mock(Foo.class);
}
@Test
public void test1() {
when(mockFoo.someMethod()).thenReturn(0);
TestObject testObj = new TestObject(mockFoo);
testObj.bar(); // calls mockFoo.someMethod(), receiving 0 as the value
}
@Test
public void test2() {
when(mockFoo.someMethod()).thenReturn(1);
TestObject testObj = new TestObject(mockFoo);
testObj.bar(); // calls mockFoo.someMethod(), STILL receiving 0 as the value, instead of expected 1.
}
}
在第二个测试中,当调用 testObj.bar() 时,我仍然收到 0 作为值...解决此问题的最佳方法是什么?请注意,我知道我可以在每个测试中使用不同的 Foo
模拟,但是,我必须将多个请求链接到 mockFoo
上,这意味着我必须执行以下操作每个测试中的链接。
So, I'm creating a mock object as a static variable on the class level like so... In one test, I want Foo.someMethod()
to return a certain value, while in another test, I want it to return a different value. The problem I'm having is that it seems I need to rebuild the mocks to get this to work correctly. I'd like to avoid rebuilding the mocks, and just use the same objects in each test.
class TestClass {
private static Foo mockFoo;
@BeforeClass
public static void setUp() {
mockFoo = mock(Foo.class);
}
@Test
public void test1() {
when(mockFoo.someMethod()).thenReturn(0);
TestObject testObj = new TestObject(mockFoo);
testObj.bar(); // calls mockFoo.someMethod(), receiving 0 as the value
}
@Test
public void test2() {
when(mockFoo.someMethod()).thenReturn(1);
TestObject testObj = new TestObject(mockFoo);
testObj.bar(); // calls mockFoo.someMethod(), STILL receiving 0 as the value, instead of expected 1.
}
}
In the second test, I'm still receiving 0 as the value when testObj.bar() is called... What is the best way to resolve this? Note that I know I could use a different mock of Foo
in each test, however, I have to chain multiple requests off of mockFoo
, meaning I'd have to do the chaining in each test.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您还可以 存根连续调用(2.8.9 api 中的#10)。在这种情况下,您将使用多个 thenReturn 调用或一个带有多个参数 (varargs) 的 thenReturn 调用。
You could also Stub Consecutive Calls (#10 in 2.8.9 api). In this case, you would use multiple thenReturn calls or one thenReturn call with multiple parameters (varargs).
对于所有搜索返回某些内容然后再次调用抛出异常的人:
或
For all who search to return something and then for another call throw exception:
or
首先不要使模拟静态。将其设为私有字段。只需将您的 setUp 类放在
@Before
中,而不是@BeforeClass
中。它可能会运行很多,但很便宜。其次,您现在的方式是让模拟根据测试返回不同内容的正确方法。
First of all don't make the mock static. Make it a private field. Just put your setUp class in the
@Before
not@BeforeClass
. It might be run a bunch, but it's cheap.Secondly, the way you have it right now is the correct way to get a mock to return something different depending on the test.
或者,更干净:
Or, even cleaner:
对于任何使用spy()和doReturn()而不是when()方法的人:
你需要在不同的调用上返回不同的对象是这样的:
。
对于经典模拟:
或者抛出异常:
For Anyone using spy() and the doReturn() instead of the when() method:
what you need to return different object on different calls is this:
.
For classic mocks:
or with an exception being thrown: