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

前端 未结 2 1302
醉梦人生
醉梦人生 2020-12-30 05:10

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

I have already gone through the JavaDoc but didn\'t get

相关标签:
2条回答
  • 2020-12-30 05:29

    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 ...

    0 讨论(0)
  • 2020-12-30 05:50

    really important to note that the two annotations you are referencing come from Spring Boot, not Mockito (even though that spring lib theyre from is based on Mockito)

    mixing test frameworks can give confusing and inaccurate results.

    0 讨论(0)
提交回复
热议问题