I have the following code:
@Component
public class Wrapper
{
@Resource
private List<Strategy> strategies;
public String getName(String id)
{
return strategies.stream()
.filter(strategy -> strategy.isApplicable(id))
.findFirst().get().getAmount(id);
}
}
@Component
public class StrategyA implements Strategy{...}
@Component
public class StrategyB implements Strategy{...}
I would like to create a test for it using Mockito. I wrote the test as follows:
@InjectMocks
private Wrapper testedObject = new Wrapper ();
@Mock
private List<Strategy> strategies;
@Mock
StrategyA strategyA;
@Mock
StrategyB strategyB;
@Test
public void shouldReturnNameForGivenId()
{
testedObject.getName(ID);
}
I get a NullPointerException in the line:
filter(strategy -> strategy.isApplicable(id))
which states that the list of "strategies" is initialized but empty. Is there a way Mojito will behave just like Spring? Adding automatically all instances that implement the Strategy interface to the list?
Btw I have no settings in the Wrapper class, and I would like to leave it that way, if possible.
source
share