What is difference between @SpyBean and @MockBean in Mockito?

喜你入骨 提交于 2019-12-10 02:14:06

问题


What is the difference between the @SpyBean and @MockBean annotations in Mockito?

I have already gone through the JavaDoc but didn't get the difference. If possible please give an example when to use MockBean and when SpyBean.


回答1:


A mock (no matter if we talk about ordinary objects or beans) is simply an "empty shell".

That mock object doesn't have any relation to the underlying production code. It is an object that looks like being an object of class X. But none of the methods or fields that X has do "really" exist on that mocked thing.

Whereas a spy wraps around an existing object of your class under test. Meaning: when you create a spy, you can decide if method calls going to the spy should be "intercepted" (then you are using the spy as if it would be a mock); or be "passed through" to the actual object the spy wraps around.

See here for some bean specific examples. Or there ...



来源:https://stackoverflow.com/questions/43999517/what-is-difference-between-spybean-and-mockbean-in-mockito

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!