Java Mockito - How Mock private member that initializing in the constructor

Viewed 1251

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 Answers
Related