用例:
我想用对象列表调用模拟类的方法,并想修改传递的参数。如果我使用某些返回类型的方法并且不适用于 Void 返回方法,则它工作正常。
测试方法:
public class TestClass{
@Autowired
PersistActivity activity;
public void methodToTest(List<URLObject> urlObjects) {
List<URLObject> urlObjects2PerformAction = new ArrayList<URLObject>();
for (URLObject urlObject : urlObjects) {
// Added this check to make sure, we are not running a previous
// activity which is successful.
if (urlObject.getRetryCountMap().get(currentActivityName) != null) {
urlObjects2PerformAction.add(urlObject);
}
}
boolean activityStatus = true;
try {
log.info("Calling activity " + currentActivityName + " with " + urlObjects2PerformAction);
activity.doActivity(urlObjects2PerformAction);
log.info("UrlObjects are " + urlObjects2PerformAction);
} catch (Exception e) {
//another set of actions.
}
for (URLObject urlObject : urlObjects) {
if (!urlObject.isActivitySuccessful()) {
// If complete call failed or partial data failed, update
log.info("Failed for message " + urlObject);
} else {
log.info("Passed for message " + urlObject);
}
}//end for
}//end method
}//end class
在此示例中,我想修改 urlObjects 以将其布尔变量之一填充为真/假。
我doAnswer()
在测试用例中添加了一个子句,如下所示:
测试类:
public class TestMe{
@Mock
private PersistActivity activity;
@Captor
private ArgumentCaptor<List<URLObject>> argument;
@InjectMocks
private DCUrlPersistor dcUrlPersisor = new DCUrlPersistor();
@Test
public void runIndividualDeamonWithMultipleMessagesSomeFailing() throws Exception {
URLObject urlObject1 = getUrlObject(normalizedUrl, normalizedUrl, scraperName);
URLObject urlObject2 = getUrlObject("test2.com", "test2.com", "gpsUS");
Map<String,Integer> retryCountMap = new HashMap<String, Integer>();
retryCountMap.put(PersisterActivitiesEnum.CSI.getName(), 0);
urlObject1.setRetryCountMap(retryCountMap);
urlObject2.setRetryCountMap(retryCountMap);
/**
* Change argument passed to the method.
*/
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation)
throws Throwable
{
Object[] arguments = invocation.getArguments();
if ( arguments != null
&&
arguments.length > 0
&&
arguments[0] != null)
{
List<URLObject> objs = (List<URLObject>) arguments[0];
Assert.assertNotNull(objs.get(0).getRetryCountMap());
objs.get(0).setActivitySuccessful(false);
}
return null;
}
}).when(activity).doActivity(anyList());
doNothing().when(activity).doActivity(Arrays.asList(urlObject1, urlObject2));
/**
* Call method.
*/
dcUrlPersisor.methodToTest(Arrays.asList(urlObject1, urlObject2));
/**
* Verify various instances and logs.
*/
verify(activity, times(1)).doActivity(argument.capture());
assertEquals(2, argument.getValue().size());
assertEquals(normalizedUrl, argument.getValue().get(0).getNormalizedURL());
Assert.assertTrue(logContains("Calling activity csi with "));
//-----This is the part which fails as log never prints this message----//
Assert.assertTrue(logContains("Failed for message"));
}
}
问题:
如果我将doActivity
方法的返回类型更改为List<ABC>
than Void
,那么它会被调用并按预期工作(不更改测试代码)。
我们需要对 void 方法进行一些更改吗?我已经将它设置为在 doAnswer 中返回 void。
任何帮助,将不胜感激。