Mockito - Injecting a List of mocks

后端 未结 5 816
庸人自扰
庸人自扰 2020-12-31 00:50

I have the following code:

@Component 
public class Wrapper
{ 
    @Resource 
    private List strategies;

    public String getName(String          


        
5条回答
  •  猫巷女王i
    2020-12-31 01:04

    You should not mock collections.

    Create the mocks you need and put them into a list:

    private List strategies; // not mocked!
    
    @Mock
    StrategyA strategyA;
    
    @Mock
    StrategyB strategyB;
    
    @Before
    public void setup(){
      strategies= Arrays.asList(strategyA,strategyB);
      testedObject.strategies= strategies;
    }
    
    @Test
    public void shouldReturnNameForGivenId()
    {   // irrevelant code...
        //when
        testedObject.getName(ID);
    }
    

提交回复
热议问题