Java 基于powermockito的静态模拟方法
我有一个班 具有静态功能Java 基于powermockito的静态模拟方法,java,junit,mockito,powermockito,Java,Junit,Mockito,Powermockito,我有一个班 具有静态功能 public static HashMap<String, String> loadLanguageCodeFile(HashMap<String,String> hash_map) { SystemSettings settings; FileReader fr = null; BufferedReader br = null; try { settings = SystemSettings.Get
public static HashMap<String, String> loadLanguageCodeFile(HashMap<String,String> hash_map) {
SystemSettings settings;
FileReader fr = null;
BufferedReader br = null;
try {
settings = SystemSettings.GetInstance();
String path = settings.getLangCodePath();
fr = new FileReader(path + FILENAME);
br = new BufferedReader(fr);
String Line;
while ((Line = br.readLine())!= null) {
String[] lang_codes = Line.split("\\s+");
hash_map.put(lang_codes[0], lang_codes[1]);
}
} catch (IOException e) {
log.error("MicrosoftEngine: Unable to load file.", e);
} catch (WorldlingoException e){
log.error("MicrosoftEngine:", e);
}
finally {
try {
if (fr != null) {
fr.close();
}
if (br != null) {
br.close();
}
} catch ( IOException e) {
log.error("MicrosoftEngine : An error occured while closing a resource.", e);
}
}
return hash_map;
}
`提供另一个类的实例,并在path中包含类似\var\log文件的路径文件
我正在尝试使用mockito编写一个测试用例。因为它是一个静态类,所以我使用了powermockito
@RunWith(PowerMockRunner.class)
@PrepareForTest({HttpClientBuilder.class,Engine.class, SystemSettings.class})
public class EngineTest extends TestCase {
public void testLoadLanguageCodeFile() throws Exception {
PowerMockito.mockStatic(Engine.class);
PowerMockito.mockStatic(SystemSettings.class);
MicrosoftEngine MSmock = Mockito.mock(Engine.class);
SystemSettings SystemSettingsMock = Mockito.mock(SystemSettings.class);
Mockito.when(SystemSettingsMock.GetInstance()).thenReturn(SystemSettingsMock);
HashMap<String, String> hash_map = new HashMap<String, String>();
MSmock.loadLanguageCodeFile(hash_map);
}
@RunWith(PowerMockRunner.class)
@PrepareForTest({HttpClientBuilder.class,Engine.class,SystemSettings.class})
公共类EngineTest扩展了TestCase{
public void testLoadLanguageCodeFile()引发异常{
mockStatic(Engine.class);
mockStatic(SystemSettings.class);
MicrosoftEngine MSmock=Mockito.mock(Engine.class);
SystemSettings SystemSettingsMock=Mockito.mock(SystemSettings.class);
Mockito.when(SystemSettingsMock.GetInstance())。然后返回(SystemSettingsMock);
HashMap hash_map=新HashMap();
loadLanguageCodeFile(散列映射);
}
我无法调用上述loadLanguageCodeFile方法。如果您建议如何调用静态方法,我们将不胜感激。您不必模拟测试对象。您可以模拟测试对象的依赖关系,这些依赖关系是完成测试所需的
代码还与文件读取器和缓冲区读取器等实现问题紧密耦合 但是,如注释所示,您希望在模拟设置提供的路径上测试文件的实际读取 在这种情况下,您只需要模拟
SystemSettings
,并且应该调用被测试的实际成员
RunWith(PowerMockRunner.class)
@PrepareForTest({SystemSettings.class})
public class EngineTest extends TestCase {
public void testLoadLanguageCodeFile() throws Exception {
//Arrange
String path = "Path to test file to be read";
PowerMockito.mockStatic(SystemSettings.class);
//instance mock
SystemSettings settings = Mockito.mock(SystemSettings.class);
Mockito.when(settings.getLangCodePath()).thenReturn(path);
//mock static call
Mockito.when(SystemSettings.GetInstance()).thenReturn(settings);
HashMap<String, String> hash_map = new HashMap<String, String>();
//Act
HashMap<String, String> actual = Engine.loadLanguageCodeFile(hash_map);
//Assert
//perform assertion
}
}
RunWith(PowerMockRunner.class)
@PrepareForTest({SystemSettings.class})
公共类EngineTest扩展了TestCase{
public void testLoadLanguageCodeFile()引发异常{
//安排
String path=“要读取的测试文件的路径”;
mockStatic(SystemSettings.class);
//实例模拟
SystemSettings=Mockito.mock(SystemSettings.class);
Mockito.when(settings.getLangCodePath())。然后返回(path);
//模拟静态调用
Mockito.when(SystemSettings.GetInstance())。然后返回(settings);
HashMap hash_map=新HashMap();
//表演
HashMap-actual=Engine.loadLanguageCodeFile(hash_-map);
//断言
//执行断言
}
}
参考你不应该模拟被测对象。你模拟被测对象的依赖关系,这是完成测试所需的。代码还与实现问题紧密耦合,如文件读取器和缓冲区读取器。你计划在测试时读取实际文件吗?我同意@Nkosi;我不同意除了IO片段,在这里看到任何可以模仿的东西。这确实提出了一个强烈的问题,即
FILENAME
甚至从何而来。你实际上想用这个方法测试什么?@LaxmiKadariya调用被测的实际静态成员。即HashMap result=Engine.loadLanguageCodeFile(hash\u-map);
如果我有loadLanguageCodeFile是私有的怎么办?@LaxmiKadariya
RunWith(PowerMockRunner.class)
@PrepareForTest({SystemSettings.class})
public class EngineTest extends TestCase {
public void testLoadLanguageCodeFile() throws Exception {
//Arrange
String path = "Path to test file to be read";
PowerMockito.mockStatic(SystemSettings.class);
//instance mock
SystemSettings settings = Mockito.mock(SystemSettings.class);
Mockito.when(settings.getLangCodePath()).thenReturn(path);
//mock static call
Mockito.when(SystemSettings.GetInstance()).thenReturn(settings);
HashMap<String, String> hash_map = new HashMap<String, String>();
//Act
HashMap<String, String> actual = Engine.loadLanguageCodeFile(hash_map);
//Assert
//perform assertion
}
}