I am trying to partial mock on a local variable. Then trying doReturn() so that it does not calls the original method. However it does call the original method.
Here is my doReturn statement
@Spy
ReportFileWriter iReportFileWriter = new ReportFileWriter(Any.stringNotNull(), Any.stringNotNull());
@Mock
Environment environment;
@InjectMocks
IDataSync iDataSync ;
@Test
public void testRunTask() {
doReturn(Any.stringNotNull()).when(iReportFileWriter).writeResult(any(List.class), any(Date.class));
iDataSync.someMethod(readParameters, false);
}
Any
is a custom class providing dummy values.
Any.listContactRecords()
returns a dummy list of type List<ContactRecords>
ContactRecords
is a custom class
result.contactRecords is of type :
public List<ContactRecord> contactRecords;
and startDateTime is :
public Date startDateTime;
In the code in test report file writer is a local variable, defined as
public someMethod(){
ReportFileWriter iReportFileWriter = new ReportFileWriter(environment.getProperty("reports.tempFileDir"), environment.getProperty("reports.fileNameFormater.i")) ;
fullFilename = iReportFileWriter.writeResult(result.contactRecords, taskParameter.startDateTime);
.
.
.
}
However, writeResult should have not been called. It is neither final nor static.
I should be able to get a return value according to Mockito: Trying to spy on method is calling the original method
and
I should be able to spy on local variable according to :