java - Spring(@SpyBean)与 Mockito(@Spy)

标签 java spring unit-testing kotlin spy

@SpyBean 之间有什么区别?来自 org.springframework.boot.test.mock.mockito.SpyBean@Spy来自 org.mockito.Spy

使用 @SpyBean而不是 @Spy让我的tests失败。

最佳答案

@Spy doc说:

A field annotated with @Spy can be initialized explicitly at declaration point. Alternatively, if you don't provide the instance Mockito will try to find zero argument constructor (even private) and create an instance for you.

@SpyBean doc说:

Annotation that can be used to apply Mockito spies to a Spring ApplicationContext.

All beans in the context of the same type will be wrapped with the spy. If no existing bean is defined a new one will be added.

所以主要区别在于 @SpyBean 是 Spring Boot 特定注释,而 @Spy 是 Mockito 本身的一部分。 @SpyBean@Spy 基本上做同样的事情,但是 @SpyBean 可以解决 Spring 特定的依赖关系,例如@Autowired, @Spy 只能创建空构造函数的对象。

关于java - Spring(@SpyBean)与 Mockito(@Spy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54709589/

相关文章:

javascript - 在单元测试 AngularJS 服务时注入(inject)依赖服务

java - 类之间丢失数据

java - 有没有办法从不同的方法访问数组?

java - 在多 war Spring 应用程序中使用共享父应用程序上下文

java - 如何为restTemplate实现重试机制

Python/Django - 是否有类似于 Rails 的 assert_difference 的断言?

java - 使用路径名加载图像文件时出现 NullPointerException

java - 无效条目压缩大小

java - 获取表格:options value in AJAX

mongodb - 如何在 MongoDB 上模拟 'error' 事件