[英]Mockito ArgumentCaptor is null
I'm a beginner, please bear with me.我是初学者,请多多包涵。 :)
:)
Scenario: JUnit testing with Mockito on IntelliJ / Maven.场景:JUnit 在 IntelliJ / Maven 上使用 Mockito 进行测试。 I'm trying to read a passed parameter with an ArgumentCaptor in Java.
我正在尝试使用 Java 中的 ArgumentCaptor 读取传递的参数。 The problem is, this captor itself is null.
问题是,这个俘虏本身就是 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.我怀疑问题是,由于其他原因,我不能使用 @ExtendWith(MockitoExtension.class) 。 (I have some stubs placed in a @BeforeEach method and it throws me UnnecessaryStubbingExceptions.)
(我在 @BeforeEach 方法中放置了一些存根,它抛出了 UnnecessaryStubbingExceptions。)
Is there a way to use th ArgumentCaptor without @ExtendWith?有没有办法在没有@ExtendWith 的情况下使用 th ArgumentCaptor? 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!
提前感谢您的帮助!
Indeed, the @Captor
annotation only works when you enable the MockitoExtension.实际上,@Captor 注释仅在您启用
@Captor
时才有效。
But you can also manually initialize the ArgumentCaptor without the @Captor
annotation:但是您也可以手动初始化 ArgumentCaptor 而不使用
@Captor
注释:
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.或者,如果您只需要避免 UnnecessaryStubbingExceptions,您可以在
@ExtendWith(MockitoExtension.class)
注释上方添加@MockitoSettings(strictness = Strictness.LENIENT)
Strictness.LENIENT),并继续使用@Captor/@Mock。
@ExtendWith(MockitoExtension.class)
is not required to use @Captor
with JUnit4. @ExtendWith(MockitoExtension.class)
不需要将@Captor
与 JUnit4 一起使用。 This is a JUnit5 feature and I am still using JUnit4.这是一个 JUnit5 功能,我仍在使用 JUnit4。
For me the use of @Capture
annotation failed as you described as well as the ArgumentCaptor.forClass(Invoice.class)
对我来说,正如您所描述的那样,使用
@Capture
注释以及ArgumentCaptor.forClass(Invoice.class)
失败
Unfortunately the class I was trying to capture had methods that were final and it seems Mockito was just returning null... I needed to rework how I tested to avoid the class with the final. Unfortunately the class I was trying to capture had methods that were final and it seems Mockito was just returning null... I needed to rework how I tested to avoid the class with the final.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.