简体   繁体   中英

What is the difference between stub() vs when()?

I'm a bit confused with stubbing syntax.

what is the difference between:

stub(mock.someMethod()).toReturn(10);

and

when(mock.someMethod()).toReturn(10);

According to the stub method's documentation :

Some users find stub() confusing therefore Mockito.when(Object) is recommended over stub() .

So basically they are the same . But as the stub method returns a DeprecatedOngoingStubbing (note the word deprecated ), the when method is obviously meant to be used.

As specified in the official Mockito's tutorial :

when() is a successor of deprecated stub(Object)

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