How to verify a non-mocked method was called? [duplicate]
Asked Answered
R

1

5

I want to test that my method calls another method in the same class that I cannot mock.

Example:

public void methodToTest(){

//other stuff to test that can be mocked
someClassICanMock.doSomething();

//method within same class that cannot be mocked
methodFromSameClassIWantToVerify();

}

How can I use a verify to check that this my method under test calls methodFromSameClassIWantToVerify();?

Edit: not a duplicate as I am specifically refers to how to test this using mockito.

Roasting answered 11/8, 2016 at 11:15 Comment(1)
Sounds like you want a spy, like shown here https://mcmap.net/q/2029572/-mockito-verify-a-function-is-invoked-once-in-my-caseWiles
A
11

like this,

MyClass c = new MyClass(); 
someClassICanMock  m = mock(someClassICanMock.class);
doNothing().when(m).doSomething();
MyClass s = spy(c);
s.methodToTest();
verify(s , times(1)).methodFromSameClassIWantToVerify();
Anthropomorphize answered 11/8, 2016 at 12:9 Comment(4)
thanks, so this wont actually run the code inside the method?Roasting
it will, spy allows you to inspect calls on actual instancesAnthropomorphize
ok, is there a way to call spy to see that the method was invoked, but not actually call the code inside it?Roasting
verify(s , times(1)).methodFromSameClassIWantToVerify();Anthropomorphize

© 2022 - 2024 — McMap. All rights reserved.