I'm trying to mock a SOAP Interceptor class, where one of the class methods returns an Iterator object. However, after double checking the syntax, the iterator is not replaced with the real iterator, and Mockito continues to run the method without the real iterator.
I've tried mocking the return value of the interceptor using the various mockito methods (doReturn, when... thenReturn) and neither of the methods have worked. I'm not sure where my error is in my mocking.
Here is how I mock the current object within my test class:
@Mock private WebServiceTemplate template;
@Mock private SoapInterceptor interceptor;
@Mock private Iterator<Attachment> iterator;
@Test
public void testGetDocsSoapClient() {
@SuppressWarnings("unchecked")
Iterator<Attachment> realIterator = new ArrayListIterator();
ObjectFactory realFactory = new ObjectFactory();
assertFalse(realIterator.hasNext());
doReturn(realFactory.createAwsGetDocsRequest(createMockAwsGetDocsReq()))
.when(factory).createAwsGetDocsRequest(any (AwsGetDocsRequest.class));
doReturn(realFactory.createAwsGetDocsResponse(createAwsGetDocsResponse()))
.when(template).marshalSendAndReceive(any(Object.class), any(SoapActionCallback.class));
doReturn(realIterator)
.when(interceptor).getSoapAttachments();
Here is how the method is called within the real class.
Iterator<Attachment> soapAttachments = attachmentInterceptor.getSoapAttachments();
ImageListDVO imgList = convertToImageList(soapAttachments);
... and my test case fails at the last line of this private method.
private ImageListDVO convertToImageList(Iterator<Attachment> attachments) {
ImageListDVO imgList = new ImageListDVO();
while(attachments.hasNext()) {
I should be mocking the object correctly, but I am getting a NullPointerException, which indicates that the object is not being mocked nor injected correctly.
Arrays.asList(/* whatever elements */).iterator()
. – Andy Turner