用例:
我想调用一个带有对象列表的模拟类的方法,并想修改传递的参数。如果我处理一些具有返回类型的方法,而不适用于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以将其布尔变量之一填充为true/false。
我在测试用例中添加了一个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>
而不是Void
,那么它将被调用并按预期工作(而不更改测试代码)。
我们需要对void方法进行一些更改吗?我已经在doAnswer中将其设置为返回void。
如有任何帮助,我们将不胜感激。
尝试删除以下行:
doNothing().when(activity).doActivity(Arrays.asList(urlObject1, urlObject2));
对我来说,这似乎与之前的doAnswer
设置有点相反。
像这样将Void
更改为Object
。
...
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
...