代码之家  ›  专栏  ›  技术社区  ›  sromit

Junit Mockito测试用例提供方法X为类型提供模糊性

  •  0
  • sromit  · 技术社区  · 8 年前

    @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());
    

    知道我做错了什么吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Dawood ibn Kareem    8 年前

    你应该更换线路

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

    具有

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

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