6

I would like to be able to step through a debugger, in particular Eclipse, on a Mockito Spy object. As an example, say I have the class to test below:

public class someClass(){
    public someMethod(){
        System.out.println("Begin someMethod call");
        //some code in the method
        System.out.println("End someMethod call");
    }
}

And let's say I have a Mockito class as below:

@RunWith(MockitoJUnitRunnerclass)
public class SomeMethodTest {

@Spy SomeClass someSpiedObject;

@Test
public final void testSomeMethod(){
    //some code in the test method
    someSpiedObject.someMethod();
    /some tests
}

The issue that I've run into is that if I set a debug breakpoint at //some code in the test method and at //some code in the method, the first breakpoint will be reached, but the second is not. I know that the code is run however as the system output is printed to the console. I know that, as a spied object, it is NOT the same code and the debugger shouldn't work. I also know that, VerboseLogging() could be used if it was a Mock object and not Spy. My real question is, how would one go about stepping through the code as if it were a normal object? Are there debugging frameworks that could help? Is there a feature of Mockito I'm not aware of? I eventually will no longer need it to be a Spy object and can simply test it, but not until the static calls are refactored.

Michael Rountree
  • 175
  • 2
  • 12

2 Answers2

0

I ran into the same problem, on IntelliJ. I am mocking only 1 of the methods in my service under test.

The only solution I have found is to put a breakpoint in my test code, and Step-Into (F7) the real method to debug the actual code. It sucks but it's the only way I've found so far.

UltimaWeapon
  • 690
  • 9
  • 8
0

Strange behavior, but this works for me:

  1. Clear all your spy breakpoints.
  2. Set a breakpoint in your test just before you call your spy method.
  3. Start the JUnit debugger.
  4. NOW set your breakpoints in your spy code, while the debugger is running and sitting at the breakpoint in your test code.
  5. Continue execution, and your spy breakpoints should be recognized now. Very bizarre, but whatever. Works (for me) now. Good luck!
terranche
  • 65
  • 5