als statisch (d. h. als Klassenvariable) deklariert
Code: Select all
public class EmailNotificationWorkItemHandler extends AbstractLogOrThrowWorkItemHandler {
private static Utils utils = new Utils();
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
// Throw an error due to many missing parameters on the workitem
String id= (String) workItem.getParameter("ID");
...
try {
RequiredParameterValidator.validate(this.getClass(), workItem);
...
} catch (Throwable e) {
utils.insertErrors(id, errorCode, errorMessage, e.getStackTrace(), -1L); // testing this method called
}
...
}
Code: Select all
@Test
public void executeWorkItemMissingParametersTest() {
Utils mockUtils = mock(Utils.class);
WorkItemManager mockWorkItemMgr = mock(WorkItemManager.class);
EmailNotificationWorkItemHandler mockWorkItemHandler = mock(EmailNotificationWorkItemHandler.class);
doAnswer(new Answer() {
public Void answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
System.out.println("called with arguments: " + Arrays.toString(args));
return null;
}
}).when(mockUtils).insertErrors(any(), any(), any(), any(), anyLong());
try {
doAnswer(new Answer() { // Unfinished stubbing detected
public Void answer(InvocationOnMock invocation) {
return null;
}
}).when(mockUtils);
Utils.init();
} catch (Exception e) {
System.out.println("In mocking of Utils.init() " + e.getLocalizedMessage());
}
WorkItemImpl workItem = new WorkItemImpl();
workItem.setParameter("ID", "1111-AAAA");
// Lots of required parameters removed to cause an exception and insertErrors to be called
mockWorkItemHandler.executeWorkItem(workItem, mockWorkItemMgr);
verify(mockUtils).insertErrors(any(), any(), contains("RequiredParameterValidator"), any(), anyLong());
}
Bitte beachten Sie, dass ich scheinbar alle relevanten Teile von EmailNotificationWorkItemHandler und Utils gezeigt habe.