new时,powermock,theTreturn等同于莫科托(Mockito)和junit5
我们从PowerMock
和Junit
4到Mockito
和Junit5
。我有一个非常简单的设置课。
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的测试类中可能有一个字段,例如:
或
有关您共享的PowerMockito代码的:
不久,它的作用是:
expressCredItcardClient
类的存根特定构造函数带有某些参数值。如果
expressCredItcardClient
类是通过采用单个参数的构造函数实例化的,并且该参数等于prefs
,那么而不是执行实际代码,它将返回一个模拟的实例ExpressCredItCardClient
类(ExpressClient
)。涉及Junit5/Mockito方法:
这个使用任何参数值的构造函数模拟了任何构造函数。
Probably there is a field in your test class, such as:
or
Regarding powermockito code that you shared:
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 toprefs
, then instead of executing actual code it will return a mocked instance of theExpressCreditCardClient
class (theexpressClient
).Regarding junit5/mockito approach:
This one mocks any constructor with any argument values passed.