我正在为我的项目创建 junit 测试用例。我有下面的代码,我想在其中创建一个模拟,
String propertyFilePath = System.getProperty("path.to.properties");
Resource propertyFile = new FileSystemResourceLoader().getResource(propertyFilePath);
Properties properties = PropertiesLoaderUtils.loadProperties(propertyFile);
我正在使用 junit 和 mockito-core jar。我尝试使用以下代码,
System.setProperty("path.to.properties", "dummyPathToProperties"); //invalid Path
Properties properties = mock(Properties.class);
Resource propertyFile = new FileSystemResourceLoader().getResource("dummyPathToProperties");
when(PropertiesLoaderUtils.loadProperties(propertyFile)).thenReturn(properties);
使用上面的代码,它在模拟 loadProperties 方法时会引发错误。如何模拟 spring 静态类并返回我的模拟属性对象?
任何帮助将不胜感激。