Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 560
java123999 Avatar asked Feb 07 '23 09:02

java123999


1 Answers

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();
like image 165
kuhajeyan Avatar answered Feb 11 '23 01:02

kuhajeyan