我希望Mockito能够在调用给定的void方法时执行自定义操作。
说我有以下代码:
@Autowired
private ProfileService profileService;
@Autowired
private ProfileDao profileDao;
private List<Profile> profiles;
@Before
public void setup() {
Mockito.when(profileDao.findAll()).thenReturn(profiles);
Mockito.when(profileDao.persist(any(Profile.class))).thenAddProfileToAboveList...
}
@Configuration
public static class testConfiguration {
@Bean
public ProfileDao ProfileDao() {
return mock(ProfileDao.class);
}
}
假设我想将配置文件实例添加到配置文件列表中。 Mockito能做到吗?如果是这样的话?
答案 0 :(得分:27)
doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
// make the changes you need here
}})
.when(mock).someMethod();