I have been doing a bit of reading around argument captor and the more I read about it, the more I get lost. Can someone take the pain of explaining it with an example?
Asked
Active
Viewed 1.6k times
19
-
1Possible duplicate of [How to use ArgumentCaptor for stubbing?](https://stackoverflow.com/questions/12295891/how-to-use-argumentcaptor-for-stubbing) – tkruse Dec 20 '17 at 07:14
1 Answers
25
According to docs, this is deprecated. You should use factory method forClass(Class) to create captors instead to avoid NullPointerExceptions. see here
Example:
ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
verify(mock).doSomething(argument.capture());
assertEquals("John", argument.getValue().getName());

Rachel Gallen
- 27,943
- 21
- 72
- 81
-
2Thanks for the information, Rachel but I am more interested in understanding how it works. After reading couple of blogs, I am confused as to what's the right way of using it. So, I'd you could explain it with an example, it would be great. Thanks – user123 Feb 07 '13 at 20:50
-
1you should read this similar SO question http://stackoverflow.com/questions/12295891/how-to-use-argumentcaptor-for-stubbing – Rachel Gallen Feb 07 '13 at 20:56
-
-
3The no-arg constructor of the ArgumentCaptor class is deprecated. ArgumentCaptors themselves are certainly NOT deprecated. Indeed, they are an extremely useful technique. – Dawood ibn Kareem Feb 08 '13 at 04:51