最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spring boot - Why does Mockito @MockBean work, but @SpyBean throws InvalidUseOfMatchersException? - Stack Overflow

programmeradmin1浏览0评论

The code below throws

.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 1 recorded

exception when mockRepository annotated with @SpyBean, but doesn't when I annotate it with @MockBean. The reason I want to use an ArgumentMatcher is that in other tests (such as a save operation), I can't know in advance what the object to be saved will be, so I would definitely need to use an any matcher for that.

    @Test
    public void gettingAllEntitiesFailed() throws InterruptedException {
        Mockito.when(mockitoRepository.customFindAll(eq(true))).thenThrow(RuntimeException.class);
        [...]
    }

The repository called mockitoRepository is an interface (with @Repository annotation), which extends a BaseRepository, which extends .springframework.data.repository.Repository. The BaseRepository has an implementation class: BaseRepositoryImpl. The mockitoRepository hasn't got its own impl class.

I've also tried the other "form" of mocking (the doThrow-when one), but the problem still persist. The interesting thing is that when I use true instead of eq(true) it works with @SpyBean too.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论