【Quarkus】Mockito链式mock,深度mock
问题
如果实现这样的mock,直接这样是会报错的,因为when里面同时调用了两个方法,会导致response()返回的对象并不是Mock对象,从而报错
@QuarkusTest
class MockTest {
@InjectMock
RoutingContext routingContext;
static {
Mockito.when(routingContext.response().getStatusCode()).thenReturn(200);
}
}
解决
设置mock配置为深度mock:@MockitoConfig(returnsDeepMocks = true)
@QuarkusTest
class MockTest {
@InjectMock
@MockitoConfig(returnsDeepMocks = true)
RoutingContext routingContext;
static {
Mockito.when(routingContext.response().getStatusCode()).thenReturn(200);
}
}