It's very easy, but I stumbled a little, so make a note of it.
//Mocking class
public class Mock {
//Mock handling method
public void mockMethod (String inString) {
//processing
}
}
When mocking the method as above, I was making the mock as below, But the result didn't work,
Hoge hoge = new Hoge();
Mockito.doReturn(hoge).when(mock).mockMethod(Mockito.anyString());
//Execution location
String inStr;
/**
There is some kind of kettle and processing ...
**/
Hoge hoge = mock.mockMethod(inStr);
If the hoge you receive is not the return value specified in the mock and you are worried The cause was simple, It was because the argument passed to Mock # mockMethod in the execution part was null ... If you specify Mockito.anyString (), null is not included. That's why I did this.
Hoge hoge = new Hoge();
Mockito.doReturn(hoge).when(mock).mockMethod(Mockito.any());
That's it.
Recommended Posts