简体   繁体   中英

Can I mock calling class of method partially

I want to mock a method of calling class, is that possible in Mockito

Here is sample code:

private ChecksUtil serivce;

I want to mock only serivce.method( ) not the whole class.

How can I do this?

Yes this can be done. You would use spy.

ChecksUtil service = spy(new ChecksUtil ());

when(service.method()).thenReturn(someObject);
//or if method is a void
doNothing().when(service).method();

A spied object leave the object alone except for any methods that you want to mock.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM