我需要用mockito模拟以下Model Mapper Strict策略配置。
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
我在测试方法中尝试了以下操作,但出现了Null指针异常。
@Test
public void mockModelMapper(){
when(modelmapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT))
.thenReturn(modelmapper.getConfiguration());
}
提前谢谢。
正如@DCTID所提到的,您不能链接mock。您需要拆分每个方法调用,并为getConfiguration((返回的对象注入一个mock。
@Test
public void mockModelMapper(){
// Inject the configuration mock
Configuration configurationMock = mock(Configuration.class);
when(configurationMock.setMatchingStrategy(MatchingStragegies.STRICT)
.thenReturn(configuraitonMock);
when(modelmapper.getConfiguration()).thenReturn(configurationMock);
}