junit 5使用obsconstruction()。dessetting()。useconstructor()而不是powermock.whennew()。
现在,我想使用Junit 5 + Mockito 4.x版本 + Mockito-Inline 4.x版本,而不是Junit 4 + PowerMock 2.0.9,
因为Junit 5不支持PowerMock PowerMock也可以使用Mockito-Inline可以模拟静态,看起来像它不再需要PowerMock。
但是,当我使用Mockito模拟静态时,我想使用相同的效果,例如powermock.whennew(xxx.class).withargument(1,2,3,4)。
这是我的代码的一部分,它可以起作用。
@Test
void get_report_page() {
ReportPageRequest reportPageRequest = prepare_request();
prepare_reportPage(context, 9999L, pageable);
when(reportConverter.toReportSpecification(user, reportPageRequest)).thenReturn(reportSpecification);
when(PageRequest.of(1, 100)).thenReturn(pageRequest);
when(reportRepository.findAll(reportSpecification, pageRequest)).thenReturn(reportPage);
when(reportConverter.toReportPageResponse(context)).thenReturn(reportPageResponses);
pageMockedConstruction = Mockito.mockConstruction(PageImpl.class,
withSettings().useConstructor(reportPageResponses, pageable, 9999L), (mock, context) -> {
when(mock.getTotalElements()).thenReturn(123456L);
when(mock.getTotalPages()).thenReturn(1);
when(mock.getContent()).thenReturn(reportPageResponses);
});
Page<ReportPageResponse> actual = sut.getReportPage(user, reportPageRequest);
assertThat(actual.getTotalElements()).isEqualTo(123456L);
assertThat(actual.getTotalPages()).isEqualTo(1);
assertThat(actual.getContent()).isEqualTo(reportPageResponses);
}
}
我的问题是我可以验证模拟静态对象的行为,但是无法验证结果,这是我的尝试
pageMockedConstruction = Mockito.mockConstruction(PageImpl.class,
withSettings().useConstructor(reportPageResponses, pageable, 9999L), (mock, context) -> {
when(mock.getTotalElements()).thenReturn(123456L);
when(mock.getTotalPages()).thenReturn(1);
when(mock.getContent()).thenReturn(reportPageResponses);
});
// I thought here will be the same mock object
// when expected and actual will throught the Mockito.mockConstruction, but actually generate the different object
PageImpl<ReportPageResponse> expected = new PageImpl<>(this.reportPageResponses, pageable, 9999L);
Page<ReportPageResponse> actual = sut.getReportPage(user, reportPageRequest);
// Here will be wrong, because actual and expected has different hashCode
Assertions.assertThat(actual).isEqualTo(expected);
研究很多文章,但我找不到答案。
有人遇到同样的问题吗?
Now I want to use Junit 5 + Mockito 4.x version + Mockito-inline 4.x Version instead of Junit 4 + PowerMock 2.0.9
Because the Junit 5 doesn't support PowerMock also Mockito-inline can mock static, look like it doesn't need PowerMock anymore.
But when I use Mockito mock static, I want to use the same effect like Powermock.whenNew(xxx.class).withArgument(1,2,3,4).thanReturn(someThing).
This is part of my code and it can work.
@Test
void get_report_page() {
ReportPageRequest reportPageRequest = prepare_request();
prepare_reportPage(context, 9999L, pageable);
when(reportConverter.toReportSpecification(user, reportPageRequest)).thenReturn(reportSpecification);
when(PageRequest.of(1, 100)).thenReturn(pageRequest);
when(reportRepository.findAll(reportSpecification, pageRequest)).thenReturn(reportPage);
when(reportConverter.toReportPageResponse(context)).thenReturn(reportPageResponses);
pageMockedConstruction = Mockito.mockConstruction(PageImpl.class,
withSettings().useConstructor(reportPageResponses, pageable, 9999L), (mock, context) -> {
when(mock.getTotalElements()).thenReturn(123456L);
when(mock.getTotalPages()).thenReturn(1);
when(mock.getContent()).thenReturn(reportPageResponses);
});
Page<ReportPageResponse> actual = sut.getReportPage(user, reportPageRequest);
assertThat(actual.getTotalElements()).isEqualTo(123456L);
assertThat(actual.getTotalPages()).isEqualTo(1);
assertThat(actual.getContent()).isEqualTo(reportPageResponses);
}
}
And my question is I just can verify the mock static object behavior, but can't verify the result, this is my try
pageMockedConstruction = Mockito.mockConstruction(PageImpl.class,
withSettings().useConstructor(reportPageResponses, pageable, 9999L), (mock, context) -> {
when(mock.getTotalElements()).thenReturn(123456L);
when(mock.getTotalPages()).thenReturn(1);
when(mock.getContent()).thenReturn(reportPageResponses);
});
// I thought here will be the same mock object
// when expected and actual will throught the Mockito.mockConstruction, but actually generate the different object
PageImpl<ReportPageResponse> expected = new PageImpl<>(this.reportPageResponses, pageable, 9999L);
Page<ReportPageResponse> actual = sut.getReportPage(user, reportPageRequest);
// Here will be wrong, because actual and expected has different hashCode
Assertions.assertThat(actual).isEqualTo(expected);
I research so many articles, but I can't find the answer.
Have somebody encountered the same question?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
powerMock.whennew
和mockito.mockconstruction
之间的主要区别是,mokito
在每次构造构建器时实例化时都会创建一个新的模拟打电话。但是powerMock.whennew
可以配置为始终返回一个模拟,以构建几个对象。根据
您可以使用
模拟构建&lt; t&gt; .constructed()
在上下文中获取所有生成的模拟。它们可用于验证。检查行为的测试示例:
The main difference between
Powermock.whenNew
andMockito.mockConstruction
is thatMokito
creates a new mock each time when the new object is instantiating when constructor is calling. ButPowermock.whenNew
can be configured to return one mock always for the construction of several objects.According to documentation:
You can use
MockedConstruction<T>.constructed()
to get all generated mocks in context. They can be used for verification.Example of test to check behavior: