1

Assuming that with a class like:

public class A {
    public void smth {
    }
    public void smth2 {
        smth();
    }
}

We spy it with mockito and make a simple call :

A spiedA = spy(new A());
spiedA.smth2();

After that when we want to retrieve call count:

Mockito.mockingDetails(spiedA).getInvocations().size()

And it returns two calls as expected. But i want to only register outside calls without inner delegations. Is there possibility to achieve the result i demand? If only Mockito.Invocation could provide with call stack i would filter it out.

user2207495
  • 325
  • 2
  • 12

2 Answers2

0

There is Location location field in Invocation class. In has actually stack trace inside (which is actually private field, so you should make it available through reflection). And after that you can grab required information, something like that:

((LocationImpl)((LinkedList<Invocation>) invocations).get(1).getLocation()).stackTraceHolder.stackTrace

But I would really not recommend to do that, because you would not want to maintain that test in the future. You can extract smth() into different class and spy on it if you really need.

rxn1d
  • 1,236
  • 6
  • 18
  • Yea i know about that trick but it's hacky/ugly and not good approach after all. Extraction is also not an option unfortunately. – user2207495 Oct 31 '18 at 10:03
0

Ok problem solved. Wrapped in java.lang.reflect.Proxy with custom invocation handler.

user2207495
  • 325
  • 2
  • 12