Mockito.MockConstruction不返回模拟对象

发布于 2025-02-01 14:36:18 字数 1403 浏览 3 评论 0原文

我不想再使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

权谋诡计 2025-02-08 14:36:18

您可以通过 mockedConstruction.constructed()方法。它表示每个构造函数执行后创建的模拟集合。如果您的对象将被实例化3次模拟构建&lt; t&gt; .constructed()将返回每个实例化的3个不同的模拟对象。

根据文档模拟构建&lt; t&gt;

表示代表类型的任何对象构造的模拟。
在模拟结构的范围内,任何
拦截器将生成一个模拟,该模拟将按照指定准备
生成此范围时。模拟也可以通过此收到
实例。

简单的实现和下面的评论显示了如何从模拟构建中获取模拟并验证它们:

public class A {
    private final String test;

    public A(String test) {
        this.test = test;
    }

    public String check() {
        return "checked " + this.test;
    }
}

public class TestService {
    public String purchaseProduct(String param) {
        A a = new A(param);
        return a.check();
    }
}

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;

import static org.mockito.Mockito.*;

public class ConstructorMockTest {
    private MockedConstruction<A> mockAController;

    @BeforeEach
    public void beginTest() {
        //create mock controller for all constructors of the given class
        mockAController = Mockito.mockConstruction(A.class,
                (mock, context) -> {
                    //implement initializer for mock. Set return value for object A mock methods
                    when(mock.check()).thenReturn(" Constructor Mock A ");
                });
    }

    @Test
    public void test() {
        //each instantiation of class A will return new mock, which initialized by initializer from beginTest method
        //new mock will be stored to mockAController.constructed() collection of mocks
        A aObject = new A("test");
        //ensure that method check() returns mocked value
        Assertions.assertEquals(aObject.check(), " Constructor Mock A ");
        //get just created mock for class A from controller. It will be first element of mockAController.constructed() collection
        A aMock = mockAController.constructed().get(0);
        //ensure that we get correct mock from mock controller, that it is equal from new created object
        Assertions.assertEquals(aMock, aObject);
        //verify that check method was executed on Mock
        verify(aMock, times(1)).check();

        //create new A object, new mock created and stored to mockAController.constructed()
        A aObject2 = new A("test");
        //ensure that method check() returns mocked value
        Assertions.assertEquals(aObject2.check(), " Constructor Mock A ");
        //get just created mock for class A from controller, it will be second object from constructed collection
        A aMock2 = mockAController.constructed().get(1);
        //ensure that we get correct mock from mock controller, that it is equal from just created A object
        Assertions.assertEquals(aObject2, aMock2);
        //verify that check method was executed on Mock
        verify(aMock2, times(1)).check();

        //Example of testing service which creates A object
        TestService service = new TestService();
        String serviceResult = service.purchaseProduct("test");
        //ensure that service returned value  from A mock
        Assertions.assertEquals(serviceResult, " Constructor Mock A ");
        //get just created mock for class A from controller, it will be third object from constructed collection
        A aMock3 = mockAController.constructed().get(2);
        //verify that check method was executed on Mock
        verify(aMock3, times(1)).check();
    }

    @AfterEach
    public void endTest() {
        mockAController.close();
    }
}

让我们重写测试。我没有完整的代码,但是我将尝试创建一个带有注释的示例:

    @Test
    void shouldThrowException() throws Exception {
        //given
        UrlLinkDto urlLinkDto =
                UrlPartLinkDto.builder().callId("333").videoId("5555544").build();
        ArgumentCaptor<UrlPartLinkDto> argumentCaptor = ArgumentCaptor.forClass(UrlPartLinkDto.class);

        try (MockedConstruction<ObjectMapper> objectMapperMockedConstruction = mockConstruction(ObjectMapper.class,
                (objectMapper, context) -> {
                     //initialize ObjectMapper mock to throw exception when argumentCaptor will come to writeValueAsString method
                    doThrow(JsonProcessingException.class).when(objectMapper).writeValueAsString(argumentCaptor.capture());
                })) {

            //execute service for testing and catch exception
            Throwable throwable = catchThrowable(() -> partUnlinkService.unlink(urlLinkDto, false));

            //verify that inner service was executed
            verify(partLogCheckService, times(1)).checkForExistingVideo(urlLinkDto.getVideoId());

            //verify that ObjectMapper was instantiated
            Assertions.assertEquals(1, objectMapperMockedConstruction.constructed().size());
            //get mock of ObjectMapper which was instantiated during service execution
            ObjectMapper objectMapper = objectMapperMockedConstruction.constructed().get(0);
            //verify that writeValueAsString was executed
            verify(objectMapper, times(1)).writeValueAsString(argumentCaptor.capture());

            //check that exception is correct
            assertThat(throwable).isInstanceOf(JsonProcessingException.class);
        }
    }

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>

Represents a mock of any object construction of the represented type.
Within the scope of the mocked construction, the invocation of any
interceptor will generate a mock which will be prepared as specified
when generating this scope. The mock can also be received via this
instance.

Simple implementation with comments below shows how to get mocks from MockedConstruction and verify them:

public class A {
    private final String test;

    public A(String test) {
        this.test = test;
    }

    public String check() {
        return "checked " + this.test;
    }
}

public class TestService {
    public String purchaseProduct(String param) {
        A a = new A(param);
        return a.check();
    }
}

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;

import static org.mockito.Mockito.*;

public class ConstructorMockTest {
    private MockedConstruction<A> mockAController;

    @BeforeEach
    public void beginTest() {
        //create mock controller for all constructors of the given class
        mockAController = Mockito.mockConstruction(A.class,
                (mock, context) -> {
                    //implement initializer for mock. Set return value for object A mock methods
                    when(mock.check()).thenReturn(" Constructor Mock A ");
                });
    }

    @Test
    public void test() {
        //each instantiation of class A will return new mock, which initialized by initializer from beginTest method
        //new mock will be stored to mockAController.constructed() collection of mocks
        A aObject = new A("test");
        //ensure that method check() returns mocked value
        Assertions.assertEquals(aObject.check(), " Constructor Mock A ");
        //get just created mock for class A from controller. It will be first element of mockAController.constructed() collection
        A aMock = mockAController.constructed().get(0);
        //ensure that we get correct mock from mock controller, that it is equal from new created object
        Assertions.assertEquals(aMock, aObject);
        //verify that check method was executed on Mock
        verify(aMock, times(1)).check();

        //create new A object, new mock created and stored to mockAController.constructed()
        A aObject2 = new A("test");
        //ensure that method check() returns mocked value
        Assertions.assertEquals(aObject2.check(), " Constructor Mock A ");
        //get just created mock for class A from controller, it will be second object from constructed collection
        A aMock2 = mockAController.constructed().get(1);
        //ensure that we get correct mock from mock controller, that it is equal from just created A object
        Assertions.assertEquals(aObject2, aMock2);
        //verify that check method was executed on Mock
        verify(aMock2, times(1)).check();

        //Example of testing service which creates A object
        TestService service = new TestService();
        String serviceResult = service.purchaseProduct("test");
        //ensure that service returned value  from A mock
        Assertions.assertEquals(serviceResult, " Constructor Mock A ");
        //get just created mock for class A from controller, it will be third object from constructed collection
        A aMock3 = mockAController.constructed().get(2);
        //verify that check method was executed on Mock
        verify(aMock3, times(1)).check();
    }

    @AfterEach
    public void endTest() {
        mockAController.close();
    }
}

Let's rewrite your test. I do not have the full code, but I will try to create an example with comments:

    @Test
    void shouldThrowException() throws Exception {
        //given
        UrlLinkDto urlLinkDto =
                UrlPartLinkDto.builder().callId("333").videoId("5555544").build();
        ArgumentCaptor<UrlPartLinkDto> argumentCaptor = ArgumentCaptor.forClass(UrlPartLinkDto.class);

        try (MockedConstruction<ObjectMapper> objectMapperMockedConstruction = mockConstruction(ObjectMapper.class,
                (objectMapper, context) -> {
                     //initialize ObjectMapper mock to throw exception when argumentCaptor will come to writeValueAsString method
                    doThrow(JsonProcessingException.class).when(objectMapper).writeValueAsString(argumentCaptor.capture());
                })) {

            //execute service for testing and catch exception
            Throwable throwable = catchThrowable(() -> partUnlinkService.unlink(urlLinkDto, false));

            //verify that inner service was executed
            verify(partLogCheckService, times(1)).checkForExistingVideo(urlLinkDto.getVideoId());

            //verify that ObjectMapper was instantiated
            Assertions.assertEquals(1, objectMapperMockedConstruction.constructed().size());
            //get mock of ObjectMapper which was instantiated during service execution
            ObjectMapper objectMapper = objectMapperMockedConstruction.constructed().get(0);
            //verify that writeValueAsString was executed
            verify(objectMapper, times(1)).writeValueAsString(argumentCaptor.capture());

            //check that exception is correct
            assertThat(throwable).isInstanceOf(JsonProcessingException.class);
        }
    }
万劫不复 2025-02-08 14:36:18

我认为我和您有同样的问题,希望有人可以解决我们的问题。

就我而言,我还想得到一个返回对象,例如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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文