Issue
I'm a beginner, please bear with me. :)
Scenario: JUnit testing with Mockito on IntelliJ / Maven. I'm trying to read a passed parameter with an ArgumentCaptor in Java. The problem is, this captor itself is null. The captor does not seem to work at all.
@Captor
private ArgumentCaptor<Invoice> cap;
...
verify(invoiceDao).insert(cap.capture());
I suspect the problem is, that I cannot use @ExtendWith(MockitoExtension.class) due to other reasons. (I have some stubs placed in a @BeforeEach method and it throws me UnnecessaryStubbingExceptions.)
Is there a way to use th ArgumentCaptor without @ExtendWith? Or is this not the cause for my problem in the first place?
I'm sorry if I am totally lost here. Thanks for your help in advance!
Solution
Indeed, the @Captor
annotation only works when you enable the MockitoExtension.
But you can also manually initialize the ArgumentCaptor without the @Captor
annotation:
ArgumentCaptor<Xxx> argumentCaptor = ArgumentCaptor.forClass(Xxx.class);
Or, if you just need to avoid the UnnecessaryStubbingExceptions, you can add @MockitoSettings(strictness = Strictness.LENIENT)
above the @ExtendWith(MockitoExtension.class)
annotation, and keep using @Captor/@Mock.
Answered By - GeertPt
Answer Checked By - Terry (JavaFixing Volunteer)