3

I tried to mock a member that has collect function to return a Mock object instead of getting to the function itself. But, It getting into the function no matter how i tried to mock the private member.

public class BasicReportManager implements ReportManager {
    private final ReportCollector reportCollector;

    public BasicReportManager(String id) {
        this.reportCollector = new ReportCollector(id);
    }

    private void processReport(ReportType reportTypeToProcess) {
        switch (reportTypeToProcess) {
            case REPORT1:
                Sendable collect = reportCollector.collect();
        }
    }
}

The Test code -

ReportCollector reportData = Mockito.mock(ReportCollector.class);
Sendable sendable = Mockito.mock(Sendable.class);
Mockito.when(reportData.collect()).thenReturn(sendable);

But, When I call the processReport() the collect function occurs. How can I fix it?

BasicReportManager Test Building -

basicReportManager = new BasicReportManager("1");

Thank you.

2
  • We need to see (a) how your test creates an instance of BasicReportManager and (b) the implementation of reportCollector() within BasicReportManager. Commented Sep 13, 2017 at 15:30
  • I just edit the question with the created instance of BasicReportManager on the Test and Fix the copy-paste problem with the reportCollector(). I should be the reportCollector member. Thanks. Commented Sep 13, 2017 at 15:38

2 Answers 2

2

If you want to mock the instance of ReportCollector which is used by BasicReportManager then you must inject it when your test case creates BasicReportManager. For example:

ReportCollector reportData = Mockito.mock(ReportCollector.class);
Sendable sendable = Mockito.mock(Sendable.class);
Mockito.when(reportData.collect()).thenReturn(sendable);

BasicReportManager basicReportManager = new BasicReportManager("1", reportData);

// now your test invocation on BasicReportManager will use the mocked instance of ReportCollector

You could consider al alternative like providing a factory for ReportCollector but this simple fact remains: in order to let BasicReportManager use a mocked instance of ReportCollector in your test case you have to be able (somehow!) to provide BasicReportManager with that mocked instance.

Sign up to request clarification or add additional context in comments.

Comments

1

You are mocking a ReportCollector object but you never use it inside BasicReportManager.
You should change the BasicReportManager constructor:

public BasicReportManager(ReportCollector reportCollector) {
    this.reportCollector = reportCollector;
}

Then you can do:

ReportCollector reportData = Mockito.mock(ReportCollector.class);
Sendable sendable = Mockito.mock(Sendable.class);
Mockito.when(reportData.collect()).thenReturn(sendable);

basicReportManager = new BasicReportManager(reportData);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.