How to spy on a Method that belongs to an object not accessible from outside?
<p>I'm very new to Mockito and I have a situation for which I can't find a solution. There's a method that I want to test using Mockito. The problem is that inside this method, there's an object created and <strong>that object has a function</strong> that I want to mock.</p>

<p>So for example, here is a small sample code which illustrates my issue:</p>

<pre><code>public class ClassA {
    public functionDoingDBStuff() {
        //...........
    }
}

public class ClassB {
    final ClassA classAObj = null;

    public functionXYZ() {
        classAObj = new ClassA();

        classAObj.functionDoingDBStuff();
    }
}


@Test
MyTestFunction() {
    ClassB classBObj = new ClassB();

    // How can I access and mock functionDoingDBStuff() here?
}
</code></pre>

<p>So in <code>MyTestFunction()</code>, I want to test <code>functionXYZ()</code>, but mock the function <code>functionDoingDBStuff()</code> that is called inside <code>functionXYZ()</code>. By mock, I mean return a specific result that I want for the test. However function <code>functionDoingDBStuff()</code> belongs to an object that's created inside <code>functionXYZ()</code>, so I don't know how I can tell Mockito to access it from within <code>MyTestFunction()</code>. I hope you're able to understand what I mean.</p>