如何使用 Mockito 模拟 System.getProperty [英] How to Mock System.getProperty using Mockito
本文介绍了如何使用 Mockito 模拟 System.getProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 org.mockito.plugins.MockMaker 文件中添加了 mock-maker-inline 文本并将其放置在 test/resources/mockito-extensions 中
I have added mock-maker-inline text in org.mockito.plugins.MockMaker file and placed it in test/resources/mockito-extensions
在我的测试用例中,我使用:
In my test case I am using:
System system = mock(System.class);
when(system.getProperty("flag")).thenReturn("true");`
但我收到以下异常:
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);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
感谢任何建议
推荐答案
您也可以使用真实的方法,在每次测试前后准备和删除配置:
Your could also use real methods, preparing and removing the configuration before and after each test:
@Before
public void setUp() {
System.setProperty("flag", "true");
}
@After
public void tearDown() {
System.clearProperty("flag");
}
这篇关于如何使用 Mockito 模拟 System.getProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文