19

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?

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
user123
  • 281
  • 1
  • 3
  • 16
  • 1
    Possible 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 Answers1

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
  • 2
    Thanks 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
  • 1
    you 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
  • Thank you, that helps. Never came up when I was searching for it. – user123 Feb 07 '13 at 21:23
  • 3
    The 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