Mockito.MockConstruction不返回模拟对象
我不想再使用PowerMock。因为Junit5开始嘲笑静态课程。因此,我试图摆脱PowerMock方法。
如您所知,您可以使用新闻关键字创建类的实例。因此,我决定使用“无类结构”。但是,无类结构不会返回模拟的对象。它不会进入尝试块。
这是我的之前的方法:
@BeforeEach
void setUp() {
partUnlinkService =
spy(new PartVideoUnlinkService(part1, part2,
part3));
}
这是我的测试方法:
@Test
void shouldThrowException() throws Exception {
//given
UrlLinkDto urlLinkDto =
UrlPartLinkDto.builder().callId("333").videoId("5555544").build();
ArgumentCaptor<UrlPartLinkDto> argumentCaptor = ArgumentCaptor.forClass(UrlPartLinkDto.class);
//when
try (MockedConstruction<ObjectMapper> ignoredVariable = mockConstruction(ObjectMapper.class,
(objectMapper, context) -> {
//then
partUnlinkService.unlink(urlLinkDto, false);
verify(partLogCheckService, times(1)).checkForExistingVideo(
urlLinkDto.getVideoId());
verify(objectMapper, times(1)).writeValueAsString(argumentCaptor.capture());
Throwable throwable =
catchThrowable(() -> objectMapper.writeValueAsString(argumentCaptor.capture()));
assertThat(throwable).isInstanceOf(JsonProcessingException.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. So i decided to use " mockConstruction " . But mockConstruction does not return the mocked object. It doesn't go inside the try block.
This is my BeforeEach method:
@BeforeEach
void setUp() {
partUnlinkService =
spy(new PartVideoUnlinkService(part1, part2,
part3));
}
This is my test method:
@Test
void shouldThrowException() throws Exception {
//given
UrlLinkDto urlLinkDto =
UrlPartLinkDto.builder().callId("333").videoId("5555544").build();
ArgumentCaptor<UrlPartLinkDto> argumentCaptor = ArgumentCaptor.forClass(UrlPartLinkDto.class);
//when
try (MockedConstruction<ObjectMapper> ignoredVariable = mockConstruction(ObjectMapper.class,
(objectMapper, context) -> {
//then
partUnlinkService.unlink(urlLinkDto, false);
verify(partLogCheckService, times(1)).checkForExistingVideo(
urlLinkDto.getVideoId());
verify(objectMapper, times(1)).writeValueAsString(argumentCaptor.capture());
Throwable throwable =
catchThrowable(() -> objectMapper.writeValueAsString(argumentCaptor.capture()));
assertThat(throwable).isInstanceOf(JsonProcessingException.class);
})) {
}
}
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过 mockedConstruction.constructed()方法。它表示每个构造函数执行后创建的模拟集合。如果您的对象将被实例化3次
模拟构建&lt; t&gt; .constructed()
将返回每个实例化的3个不同的模拟对象。根据文档
模拟构建&lt; t&gt;
简单的实现和下面的评论显示了如何从
模拟构建
中获取模拟并验证它们:让我们重写测试。我没有完整的代码,但是我将尝试创建一个带有注释的示例:
You can access mocks that were created during the instantiation of your objects via MockedConstruction.constructed() method. It represents a collection of mocks that are created after each constructor's execution. If your object will be instantiated 3 times
MockedConstruction<T>.constructed()
will return 3 different mock objects for each instantiation.According to documentation
MockedConstruction<T>
Simple implementation with comments below shows how to get mocks from
MockedConstruction
and verify them:Let's rewrite your test. I do not have the full code, but I will try to create an example with comments:
我认为我和您有同样的问题,希望有人可以解决我们的问题。
就我而言,我还想得到一个返回对象,例如powermock.whennew()。wandarguments()。
我认为仅仅可以嘲笑他们的行为,无法像PowerMock一样验证他们的结果,而我找不到任何文章或答案。
以下是我的帖子链接:
().useconstructor()而不是powermock.whennew()。witharguments()
I think I have the same question as you, hope someone can solve our problem.
In my case, I also want to got a return object like PowerMock.whenNew().withArguments().thenReturn(someThing), I try a lot of times but it failed.
I think mockConstruction just only can mock their behavior, can't verify their result like powerMock, and I couldn't find any articles or answers.
below is my post link :
Junit 5 use mockConstruction().withSetting().useConstructor() instead of PowerMock.whenNew().withArguments()