0

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 :

Using Mockito to mock a local variable of a method

Bhavya Arora
  • 768
  • 3
  • 16
  • 35
  • Why aren't you using argument matchers when you specify the when condition? You should be using "eq(whatever)" or "any()" or similar. – Dave Jan 07 '18 at 02:30
  • I did a quick change to writeResult(eq(Any.listContactRecords()), any(Date.class)) Didn't help – Bhavya Arora Jan 07 '18 at 03:09
  • Could you please show a complete code of your test method? – Ivan Jan 08 '18 at 02:19
  • Perhaps that was not your only problem, but it was a problem. Ivan is right. You aren't showing enough code to figure out what exactly you are doing wrong. Where do you create the spy? – Dave Jan 08 '18 at 07:34
  • @Dave I added the code where I am creating a spy. – Bhavya Arora Jan 08 '18 at 20:03
  • @BhavyaArora, could you please also add whole test method? Your `someMethod()` from above doesn't use mock created with spy. – Ivan Jan 09 '18 at 01:57
  • @Ivan I updated the code above, see if it makes more sense now and if you need anything more? – Bhavya Arora Jan 09 '18 at 19:10
  • 1
    If you use local variable `iReportFileWriter` in your `IDataSync.someMethod()` then `@InjectMocks` will not help. You need to change `IDataSync.someMethod()` to either to use a property of `IDataSync` object or to accept instance of `iReportFileWriter` as a parameter – Ivan Jan 09 '18 at 19:19
  • Yeah, I get you. I am creating a property of IDataSync. As I feared that this might be your answer and might be no way to create a mock of a local variable through partial mocking. – Bhavya Arora Jan 09 '18 at 19:29

0 Answers0