【问题标题】:Junit Mockito Test case giving method X giving ambiguous for the TypeJunit Mockito 测试用例给方法 X 给类型模棱两可
【发布时间】:2017-12-03 18:34:15
【问题描述】:

我有一个包含以下代码的 repo 类

@Repository
public interface ShopComponentsFramesRepository extends JpaRepository<ShopComponentFrame, Long> {

    public List<ShopComponentFrame> findByComponentIdInAndShopId(Collection<Long> componentIdList,
            Collection<Long> shopIdList);

    public ShopComponentFrame findByComponentIdInAndShopId(Long componentId, Long shopId);

}

我有一个带有以下代码的 mockito junit 测试类,但编译器抱怨 - “方法 findByComponentIdInAndShopId(Collection, Collection) 对于 ShopComponentsFramesRepository 类型不明确"

@Test
    public void testRequestReceivingDropdownElements() {
        ReceivingDropdownsView sampleDropdownsView = getReceivingDropdownsSample();
        when(authorityManager.retrieveAuthorities(USER_NAME)).thenReturn(getSampleAuthoritiesView());
        when(shopComponentFrameConverter.convertToView(anyObject())).thenReturn(getComponentFrame());
        when(shopComponentsFramesRepository.findByComponentIdInAndShopId(anyObject(), anyObject()))
                .thenReturn(getShopComponentFrameList());
        when(shopComponentsFramesRepository.findByComponentIdInAndShopId(anyLong(), anyLong()))
                .thenReturn(getShopComponentFrame());

知道我做错了什么吗?

【问题讨论】:

  • 不确定,但请尝试将第二个重载方法中的 Long 替换为 long。 Long 仍然是一个类似于 Collection 的对象,可能会导致歧义。不过只是猜测。

标签: java spring-boot junit mockito spring-data-jpa


【解决方案1】:

你应该换行

when(shopComponentsFramesRepository.findByComponentIdInAndShopId(anyObject(), anyObject()))
            .thenReturn(getShopComponentFrameList());

when(shopComponentsFramesRepository.findByComponentIdInAndShopId(anyCollectionOf(Long.class), anyCollectionOf(Long.class)))
            .thenReturn(getShopComponentFrameList());

以便 Mockito 可以判断您要存根的方法的两个版本中的哪一个。

或者,为这两种方法使用不同的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 2020-06-21
    • 1970-01-01
    相关资源
    最近更新 更多