我想模拟 System.getenv() 方法。我只找到了 JUnit4 和 PowerMockito 的解决方案。我使用以下依赖项:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.23.0</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这是我的测试示例:
@ExtendWith(MockitoExtension.class)
public class TestEnvVariable {
@Mock
System system;
@Test
public void shouldExpandPropertyContentToMatchingSysEnv() throws Exception {
when(system.getenv("KEY")).thenReturn("VALUE");
assertEquals("VALUE", "KEY");
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用 JUnit5 模拟 System.getenv()?
出于某些测试目的,我想准确预测System.currentTimeMillis()
将返回什么.有什么方法可以冻结或手动设置System.currentTimeMillis()
调用时会返回什么?