Mockito 中的@SpyBean 和@MockBean 有什么区别? [英] What is difference between @SpyBean and @MockBean in Mockito?

查看:52
本文介绍了Mockito 中的@SpyBean 和@MockBean 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mockito 中的 @SpyBean@MockBean 注释有什么区别?

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

我已经阅读了 JavaDoc,但没有发现区别.如果可能,请举例说明何时使用 MockBean 以及何时使用 SpyBean.

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.

推荐答案

一个mock(无论我们谈论的是普通对象还是bean)只是一个空壳".

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

该模拟对象与底层生产代码没有任何关系.它是一个看起来类似于 X 类的对象的对象.但是 X 所具有的真正"的方法或字段都不存在于那个被模拟的东西上.

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.

请参阅这里 对于一些特定于 bean 的示例.或者 那里 ...

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

这篇关于Mockito 中的@SpyBean 和@MockBean 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆