【问题标题】:How can I mock a call to Spring's repository `saveAll()` method using mockk?How can I mock a call to Spring\'s repository `saveAll()` method using mockk?
【发布时间】:2022-12-27 19:21:38
【问题描述】:

I am using Mockk as my mocking framework when testing my Spring Boot Data repository interfaces.

Actually I am doing the following

every { itemRepository.saveAll(listOf(any(), any())) } returns listOf<Item>(mockk())

which should mock the following behaviour

val loot: List<Item> = itemGenerator.generateLoot(lootTable)
itemRepository.saveAll(loot)

The error message I receive is the following:

Failed matching mocking signature for
SignedCall(retValue=, isRetValueMock=true, retType=class kotlin.collections.Iterable, self=ItemRepository(#28), method=saveAll(Iterable), args=[[com.barbarus.gameserver.item.Item@ea00de, com.barbarus.gameserver.item.Item@23ca36d]], invocationStr=ItemRepository(#28).saveAll([com.barbarus.gameserver.item.Item@ea00de, com.barbarus.gameserver.item.Item@23ca36d]))
left matchers: [any(), any()]

The error message says left matchers: [any(), any()] pointing out that I somehow am not defining the expected arguments right.

I could fully define the items by real implementations in my test logic but I'd like to stick with mockk() just to keep the test code slim and fast.

However I kinda am not able to define the List&lt;Item&gt; with two elements using listOf(any(),any()) here. I tried other API of Mockk without any luck.

Any idea what to use in this case?

【问题讨论】:

    标签: kotlin mocking mockito mockk spring-repositories


    【解决方案1】:

    You should type the any() when you are passing into saveAll().

    For instance:

    import com.barbarus.gameserver.item.Item
    ...
    
    every { itemRepository.saveAll(any<List<Item>>() } returns listOf<Item>(mockk())
    

    Solution from another post

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 2022-12-27
      • 2022-12-02
      • 2022-12-27
      • 2022-11-20
      • 2022-12-01
      • 2022-11-09
      • 2022-12-02
      • 2022-12-27
      相关资源
      最近更新 更多