new时,powermock,theTreturn等同于莫科托(Mockito)和junit5

发布于 2025-02-01 14:47:29 字数 675 浏览 7 评论 0原文

我们从PowerMockJunit 4到MockitoJunit5。我有一个非常简单的设置课。

    @BeforeEach
    public void setUp() throws Exception {
    service = new ExpressProcessorService<>(prefs);
    
    //whenNew(ExpressCreditCardClient.class).withArguments(prefs).thenReturn(expressClient);
    MockedConstruction<ExpressCreditCardClient> mockedExpressCreditCardClient = mockConstruction(ExpressCreditCardClient.class);

    }

我知道Inline模拟依赖性,并且已经有了它,我已经看到了一些答案,因此不幸的是我不明白。

有人可以解释何时发表的评论时,以及如何在junit5 + mockito中进行完全相同的操作?

We are migrating from PowerMock and JUnit 4 to Mockito and JUnit5. I have a very simple setup class.

    @BeforeEach
    public void setUp() throws Exception {
    service = new ExpressProcessorService<>(prefs);
    
    //whenNew(ExpressCreditCardClient.class).withArguments(prefs).thenReturn(expressClient);
    MockedConstruction<ExpressCreditCardClient> mockedExpressCreditCardClient = mockConstruction(ExpressCreditCardClient.class);

    }

I know about inline mock dependency and already have it and I have seen a few answers in so that I unfortunately did not understand.

Could somebody explain what exactly the commented whenNew line does, and how to do the exact same in Junit5 + Mockito?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

绝對不後悔。 2025-02-08 14:47:29

您的测试类中可能有一个字段,例如:

ExpressCreditCardClient expressClient = ...creation of mock...

@Mock
ExpressCreditCardClient expressClient

有关您共享的PowerMockito代码的

nnew(expressCreditCardClient.Class).witharguments(prefs).thenreturn(ExpressClient);

不久,它的作用是:expressCredItcardClient类的存根特定构造函数带有某些参数值。

如果expressCredItcardClient类是通过采用单个参数的构造函数实例化的,并且该参数等于prefs,那么而不是执行实际代码,它将返回一个模拟的实例ExpressCredItCardClient类(ExpressClient)。

涉及Junit5/Mockito方法:

MockedConstruction<ExpressCreditCardClient> mockedExpressCreditCardClient = mockConstruction(ExpressCreditCardClient.class);

这个使用任何参数值的构造函数模拟了任何构造函数。

Probably there is a field in your test class, such as:

ExpressCreditCardClient expressClient = ...creation of mock...

or

@Mock
ExpressCreditCardClient expressClient

Regarding powermockito code that you shared:

whenNew(ExpressCreditCardClient.class).withArguments(prefs).thenReturn(expressClient);

Shortly, what it does is: stub specific constructor of ExpressCreditCardClient class with certain argument value passed.

If ExpressCreditCardClient class is instantiated through a constructor that takes a single argument, and that argument is equal to prefs, then instead of executing actual code it will return a mocked instance of the ExpressCreditCardClient class (the expressClient).

Regarding junit5/mockito approach:

MockedConstruction<ExpressCreditCardClient> mockedExpressCreditCardClient = mockConstruction(ExpressCreditCardClient.class);

This one mocks any constructor with any argument values passed.

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