【问题标题】:Mockito cannot create Spy of @Autowired Spring-Data RepositoryMockito 无法创建 @Autowired Spring-Data Repository 的间谍
【发布时间】:2019-01-16 17:26:03
【问题描述】:

我正在尝试用 Mockito.spy 功能覆盖我的整个测试环境,所以只要我想我可以存根一个方法,但所有其他调用都使用默认功能。 这在 Service 层工作得很好,但我在 Repository 层遇到了问题。

我的设置如下:

Mockito - 2.15.0 春天 - 5.0.8 SpringBoot - 2.0.4

存储库:

public interface ARepository extends CrudRepository<ADBO, Long> {}

服务:

@Service
public class AService {

    @Autowired
    ARepository aRepository;

    public ADBO getById(long id) {
        return aRepository.findById(id).orElse(null);
    }

    public Iterable<ADBO> getAll() {
        return aRepository.findAll();
    }
}

间谍的配置:

@Profile("enableSpy")
@Configuration
public class SpyConfig {

    @Bean
    @Primary
    public ARepository aRepository() {
        return Mockito.spy(ARepository.class);
    }
}

还有我的测试课:

@ActiveProfiles("enableSpy")
@RunWith(SpringRunner.class)
@SpringBootTest
public class AServiceTest {

    @Autowired
    AService aService;

    @Autowired
    ARepository aRepository;

    @Test
    public void test() {
        ADBO foo = new ADBO();
        foo.setTestValue("bar");
        aRepository.save(foo);

        doReturn(Optional.of(new ADBO())).when(aRepository).findById(1L);
        System.out.println("result (1):" + aService.getById(1));

        System.out.println("result all:" + aService.getAll());

    }
}

现在这个测试有三种可能的结果:

  • aRepository 既不是模拟也不是间谍:
    org.mockito.exceptions.misusing.NotAMockException: Argument passed to when() is not a mock! Example of corr...
  • aRepository 是模拟但不是间谍(这是我得到的结果):
    result (1):ADBO(id=null, testValue=null) result all:[]

  • aRepository 是一个间谍(这就是我想要的):
    result (1):ADBO(id=null, testValue=null) result all:[ADBO(id=1, testValue=bar)]

我将此行为归因于存储库的spring实例化在后台更复杂,并且在调用Mockito.spy(ARepository.class)时未正确实例化存储库。

我还尝试将正确的实例自动装配到配置中,并使用@Autowired 对象调用Mockito.spy()

这会导致:

Cannot mock/spy class com.sun.proxy.$Proxy75
Mockito cannot mock/spy because :
 - final class

根据我的研究,从 v2.0.0 开始,Mockito 可以模拟和监视 final 类。

调用Mockito.mockingDetails(aRepository).isSpy() 返回true,这让我认为后台的对象没有正确实例化。

最后是我的问题:

如何使用 @Autowired 在我的 UnitTest 中获取 Spring-Data Repository 的间谍实例?

【问题讨论】:

标签: java unit-testing spring-boot spring-data-jpa spy


【解决方案1】:

2021 年 8 月更新

你应该使用@SpyBean

它曾经被破坏(请参阅下面的问题链接),但现在已经修复并支持自 Spring Boot 2.5.3

如果您仍然收到此错误并且无法更新您的 Spring Boot 版本,您可能想尝试@Mateusz Zając 在此问题的答案之一中提出的解决方法。 (如果它适合你,请考虑支持他的答案)

(已关闭)问题的链接: http://github.com/spring-projects/spring-boot/issues/7033

【讨论】:

  • 它也不适用于 Feign 客户端。这是一个已知问题吗?
  • 正如@Mateusz Zając 在评论中指出的那样,Github 问题现已关闭,并且自 Spring Boot 版本 2.5.3 起支持此功能
【解决方案2】:

@SpyBean 现在适用于自 Spring Boot 版本 2.5.3

以来的 Spring 数据存储库

如果您无法升级,您可以通过在测试方法主体中实例化您的间谍来解决此问题:

var yourSpy = Mockito.mock(FooRepository.class, AdditionalAnswers.delegatesTo(real))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 2019-03-08
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    相关资源
    最近更新 更多