如何从Java 8和系统类模拟功能接口
我想在下面进行模拟:
((Function<String, String>) System::getenv).apply(TransformerConstants.TRANSFORMER_POOL_SIZE)
我尝试使用以下代码:
PowerMockito.when(((Function<String, String>) System::getenv).apply(TransformerConstants.PRECISE_ID_TRANSFORMER_POOL_SIZE)).thenReturn("dsad");
但下面给出的错误:
org.mockito.exceptions.misusing.missingmethodinvocation exception: 当()需要一个必须是“模拟方法的方法”的参数。 例如: 当(mock.getarticles())
。
I want to mock below call :
((Function<String, String>) System::getenv).apply(TransformerConstants.TRANSFORMER_POOL_SIZE)
I tried using below code :
PowerMockito.when(((Function<String, String>) System::getenv).apply(TransformerConstants.PRECISE_ID_TRANSFORMER_POOL_SIZE)).thenReturn("dsad");
But its giving below error :
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Let me know how this entire call can be mocked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要尝试模拟
系统:: getenv
。相反,构建代码,以便您可以用其他值(模拟或仅在线测试lambda)替换该方法参考:Don't try to mock
System::getenv
. Instead, structure your code so that you can replace that method reference with some other value (a mock or just an inline test lambda):我可以这样做:
I was able to do so this with :