当新的替代方案时,PowerMock
我不想再使用PowerMock。因为Junit5开始嘲笑静态课程。因此,我试图摆脱PowerMock方法。
如您所知,您可以使用新闻关键字创建类的实例。
Junit5中有其他选择吗?
这是我的代码的一部分:
whenNew(PDFDocument.class).withNoArguments().thenReturn(pdfDocument);
whenNew(PSConverter.class).withNoArguments().thenReturn(converter);
doNothing().when(pdfDocument).load(ArgumentMatchers.any(ByteArrayInputStream.class));
doAnswer(invocationOnMock -> {
ByteArrayOutputStream outputStream = invocationOnMock.getArgument(1);
outputStream.write(content);
return outputStream;
}).when(converter).convert(ArgumentMatchers.any(), ArgumentMatchers.any(ByteArrayOutputStream.class));
I don't want to use powermock anymore. Because junit5 started mocking static classes. So i am trying to get rid of powermock methods.
As you know, you can create an instance of a class with whenNew keyword.
Is there any alternative in Junit5 for whenNew?
Here is a part of my code:
whenNew(PDFDocument.class).withNoArguments().thenReturn(pdfDocument);
whenNew(PSConverter.class).withNoArguments().thenReturn(converter);
doNothing().when(pdfDocument).load(ArgumentMatchers.any(ByteArrayInputStream.class));
doAnswer(invocationOnMock -> {
ByteArrayOutputStream outputStream = invocationOnMock.getArgument(1);
outputStream.write(content);
return outputStream;
}).when(converter).convert(ArgumentMatchers.any(), ArgumentMatchers.any(ByteArrayOutputStream.class));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仿真对象构建以来,自Mockito 3.5.0根据文档。
首先,您需要添加 mockito-inline 而不是测试依赖项的
Mockito核
。Code> Mockito-Inline 提供了模拟静态或最终方法的构造函数的能力。 Mockito-Core与Mockito-Inline之间的差异
让我们创建一个简单的服务用于测试哪些实例化对象。
构造函数模拟带有评论的示例:
对于您的课堂示例:
Mocking object construction is available since Mockito 3.5.0 according to documentation.
First of all you need add the mockito-inline instead of the
mockito-core
to your test dependencies.mockito-inline
provides ability to mock static or final methods, constructors. Difference between mockito-core vs mockito-inlineLet's create a simple service for testing which instantiate objects.
Example of constructor mock with comments:
For your classes example: