Java: How do I mock a method of a field when that field isn't exposed?

Viewed 29628

I'm using Java 6, JUnit 4.8.1, and writing a console application. My application has a member field that isn't exposed …

public class MyApp { 
    ...
    private OpportunitiesService m_oppsSvc;

    private void initServices() { 
        …
        m_oppsSvc = new OpportunitiesServiceImpl(…);
    }
    ...
}

I want to mock a behavior such that whenever one method from my service is called, (e.g. m_oppsSvc.getResults()), the same result is always returned. How do I do that? There's no setter method for the field. I'm currently working with Mockito 1.8.4. Is it possible to do this with Mockito or some other mock framework?

6 Answers
Related