【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);
  }
}
posted @ 2024-02-02 10:56  小小记录本  阅读(64)  评论(0编辑  收藏  举报