Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
698 views
in Technique[技术] by (71.8m points)

mocking - Mockito issue - when(java.lang.Void) in Stubber cannot be applied to void

I can't figure out why the doNothing isn't working for this? Any ideas?

@Captor
ArgumentCaptor<GenericClass<someOtherClass>> captor;
...
Mockito.doNothing().when(mockObject.methodToStub(captor.capture()));

The error is:

Exception: when(java.lang.Void) in Stubber cannot be applied to void

question from:https://stackoverflow.com/questions/25249902/mockito-issue-whenjava-lang-void-in-stubber-cannot-be-applied-to-void

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This stub is wrong :

doNothing().when(mockObject.methodToStub(captor.capture())); // wrong

methodToStub(...) must be outside the when if using this API style (it should only contain the mock) :

doNothing().when(mockObject).methodToStub(captor.capture()); // correct

Tho remarks however :

  1. doNothing is the default for void methods for a mock.
  2. You can use the BDDMockito aliases that enables your code to be real à la Behavior Driven Development

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...