gpt4 book ai didi

java - 如何使用 Mockito 模拟 System.getProperty

转载 作者:行者123 更新时间:2023-11-30 06:45:39 25 4
gpt4 key购买 nike

我在 org.mockito.plugins.MockMaker 文件中添加了 mock-maker-inline 文本并将其放在 test/resources/mockito-extensions 中

在我的测试用例中,我使用:

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.

感谢任何建议

最佳答案

您也可以使用真正的方法,在每次测试前后准备和删除配置:

@Before
public void setUp() {
System.setProperty("flag", "true");
}

@After
public void tearDown() {
System.clearProperty("flag");
}

关于java - 如何使用 Mockito 模拟 System.getProperty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48529605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com