Mockito - Injecting a List of mocks

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

I have the following code:

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

    public String getName(String          


        
5条回答
  •  天涯浪人
    2020-12-31 01:04

    Mockito can not know that you want to put somthing in the List strategies.

    You should rethink this an do something like this

    @InjectMocks
    private Wrapper testedObject = new Wrapper ();
    
    private List mockedStrategies;
    
    @Mock
    StrategyA strategyA;
    
    @Mock
    StrategyB strategyB;
    
    @Before
    public void setup() throws Exception {
        mockedStrategies = Arrays.asList(strategyA, strategyB);
        wrapper.setStrategies(mockedStrategies);
    }
    

提交回复
热议问题