How to verify a non-mocked method was called?

java123999 picture java123999 · Aug 11, 2016 · Viewed 10.9k times · Source

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.

Answer

kuhajeyan picture kuhajeyan · Aug 11, 2016

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();