Mockito 重新存根方法已使用 thenthrow 存根
我遇到了mockito的问题。 我正在开发一个网络应用程序。在我的测试中,用户管理被嘲笑。 在某些情况下,我必须更改 getLoggedInUser() 方法返回的用户。
问题是,我的 getLoggedInUser() 方法也可能引发 AuthenticationException。
因此,当我尝试从无用户切换到某个用户时,调用会
when(userProvider.getLoggedInUser()).thenReturn(user);
引发异常,因为 userProvider.getLoggedInUser()
已被 thenTrow()
存根如何告诉 when
方法不关心异常?
提前致谢 - István
I ran into a problem with mockito.
I am developing a web application. In my tests the user management is mocked.
There are some cases when I have to alter the User returned by the getLoggedInUser()
method.
The problem is, that my getLoggedInUser()
method can also throw an AuthenticationException
.
So when I try to switch from no user to some user, the call to
when(userProvider.getLoggedInUser()).thenReturn(user);
throws an exception, as userProvider.getLoggedInUser()
is already stubbed with thenTrow()
Is there any way for to tell the when
method not to care about exceptions?
Thanks in advance - István
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在新的 Mockito 版本中,您可以使用存根连续调用来在第一个调用上抛出异常并在第二个调用上返回一个值。
https://javadoc.io/doc /org.mockito/mockito-core/latest/org/mockito/Mockito.html#10
In new Mockito versions you can use stubbing consecutive calls to throw exception on first can and returning a value on a second call.
https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#10
我对你的问题的第一反应是,听起来你在一次测试中试图做太多事情。
为了便于测试和简单化,每个测试应该只测试一件事。这与单一职责原则相同。我经常发现程序员试图在一次测试中测试多个事物,并因此遇到各种各样的问题。因此,每个单元测试方法都应遵循以下流程:
因此,就您的情况而言,我预计至少会看到两次测试。一种是
getLoggedInUser()
返回用户,另一种是getLoggedInUser()
抛出异常。这样,您在尝试模拟模拟中的不同行为时就不会遇到问题。我想到的第二个想法是不要存根。考虑使用expect代替,因为你可以设置一系列的期望。即第一个调用返回一个用户,第二个调用抛出一个异常,第三个调用返回一个不同的用户,等等。
My first reaction to your question is that it sounds like you are trying to do too much in one test.
For ease of testing and simplicity each test should test one thing only. This is the same as the Single Responsibility Principle. I often find programmers trying to test multiple things in one test and having all sorts of problems because of it. So each of your unit test methods should follow this flow:
So in your case I would expect to see at least two tests. One where
getLoggedInUser()
returns a user, and one wheregetLoggedInUser()
throws an exception. That way you will not have problems with trying to simulate different behaviour in the mock.The second thought that spring to mind is not to stub. Look into using expect instead because you can setup a series of expectation. I.e. the first call returns a user, the second call throws an exception, the third call returns a different user, etc.
要实际回答这个问题:
TestB
由于无法摆脱断言而失败。TestC 展示了如何使用
reset
方法重置模拟并删除其上的thenThrow
命令。请注意,在我拥有的一些更复杂的示例中,重置似乎并不总是有效。我怀疑这可能是因为他们使用的是
PowerMockito.mock
而不是Mockito.mock
?To actually answer this question:
TestB
fails due to the assert that you can't get rid of.TestC
shows how thereset
method can be used to reset the mock and remove thethenThrow
command on it.Note that reset doesn't always seem to work in some more complicated examples I have. I suspect it might be because they're using
PowerMockito.mock
rather thanMockito.mock
?使用
Mockito.reset()
重置任何特定的模拟,例如Mockito.reset(mock1, mock2)
检查更多详细信息:https://stackoverflow.com/a/68126634/12085680
例如:
Use
Mockito.reset()
to reset any particular mocks, for exampleMockito.reset(mock1, mock2)
Check for more detail: https://stackoverflow.com/a/68126634/12085680
For example: