使用@InjectMocks注释时,Mockito测试在验证上失败
我正在使用Mockito为Springboot应用程序编写单元测试。 我试图使用@injectMocks
,因为它是最简单,最清洁的方法。
@ExtendWith(MockitoExtension.class)
public class MyTests {
@InjectMocks
private MyService service;
@Mock
private Producer<String, RequestMessage> producer1;
@Mock
private Producer<String, RequestResult> producer2;
...
我有2个主要流 - 在1中,我正在使用producer1
来创建和发送请求,而另一个由于请求创建中的某些故障,我正在使用producter2 。
在我的单元测试中,我想验证合适的生产商:
@Test
void start_ValidData_ShouldCreateMessageAndSendIt() throws Exception {
...
verify(producer1).send(customerEmail, message);
}
@Test
void start_DataIsNotPassingValidation_ShouldSendFailureResponse() throws Exception {
...
verify(producer1, never()).send(any(), any());
verify(producer2).send(
argThat(message -> message.getResultStatus() == FAILED_IN_PARSE));
}
当我分别运行测试时,它们正在通过,但是在运行完整测试类时,测试在验证线上失败(每次不同的失败)。从我添加的日志和调试中,我可以看到代码流如预期一样。使我假设共享模拟的生产商实例正在初始化并从2个不同的异步测试中同时调用,这会导致测试在verify
上失败。
我能够使用我的服务的显式构造函数来解决问题:
@BeforeEach
public void initMocks() {
producer1 = Mockito.mock(Producer.class);
producer2 = Mockito.mock(Producer.class);
...
service = new MyService(producer1,
producer2, ...);
}
但是我想知道为什么我的测试在使用@injectMocks
注释时会失败?
编辑: 重要的是要说我已经检查过,并确保在每次运行中都会初始化生产商的实例。 另外,即使不是这样,第一个测试也应该通过。
I am writing unit tests for a SpringBoot app using Mockito.
I was trying to use the @InjectMocks
since it is the easiest and cleanest way.
@ExtendWith(MockitoExtension.class)
public class MyTests {
@InjectMocks
private MyService service;
@Mock
private Producer<String, RequestMessage> producer1;
@Mock
private Producer<String, RequestResult> producer2;
...
I have 2 main flows - in 1 I am creating and sending a request using producer1
, and in the other due to some failure in the request creation I am sending a failure message using producer2
.
In my unit tests I want to verify the right producer was called:
@Test
void start_ValidData_ShouldCreateMessageAndSendIt() throws Exception {
...
verify(producer1).send(customerEmail, message);
}
@Test
void start_DataIsNotPassingValidation_ShouldSendFailureResponse() throws Exception {
...
verify(producer1, never()).send(any(), any());
verify(producer2).send(
argThat(message -> message.getResultStatus() == FAILED_IN_PARSE));
}
When I am running the tests separately they are passing, but when running the full tests class the tests are failing on the verify line (each time a different one is failing). From the logs I was adding and from debugging, I can see that the code flow is going as expected. What made got me assume that the shared mocked producers instances are being initialized and called simultaneously from the 2 different async tests, which causes the tests to fail on the verify
.
I was able to solve the issue using an explicit constructor for my service:
@BeforeEach
public void initMocks() {
producer1 = Mockito.mock(Producer.class);
producer2 = Mockito.mock(Producer.class);
...
service = new MyService(producer1,
producer2, ...);
}
But I was wondering why my tests are failing when using the @InjectMocks
annotation?
Edit:
It is important to say that I've checked and sure the instances of the producers are being initialized in every run.
Also, even if it wasn't so the first test should have been passed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论