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.

java123999
  • 6,974
  • 36
  • 77
  • 121

1 Answers1

10

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();
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71