使用 powermockito 模拟静态方法
Mocking static method using power mockito
我有一个classEngine.class
具有静态功能
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;
}
我正在尝试为此方法编写一个测试用例。系统设置是另一个 class 和
settings = SystemSettings.GetInstance();
String path = settings.getLangCodePath();
` 给出了另一个 class 的实例,并在路径 .
中包含类似 \var\log 文件的路径文件
我正在尝试使用 mockito 编写测试用例。由于它是静态的 class,我使用了 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);
}
我无法调用上面的 loadLanguageCodeFile 方法。任何有关如何调用静态方法的建议将不胜感激
你不应该嘲笑被测对象。您模拟被测对象的依赖项,这些依赖项是完成测试所需的。
该代码还与文件 reader 和缓冲区 reader 等实现问题紧密耦合。
然而,正如评论中所指出的,您想要在模拟设置提供的路径上测试文件的实际读取。
在那种情况下你只需要模拟 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
}
}
我有一个classEngine.class
具有静态功能
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;
}
我正在尝试为此方法编写一个测试用例。系统设置是另一个 class 和
settings = SystemSettings.GetInstance();
String path = settings.getLangCodePath();
` 给出了另一个 class 的实例,并在路径 .
中包含类似 \var\log 文件的路径文件我正在尝试使用 mockito 编写测试用例。由于它是静态的 class,我使用了 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);
}
我无法调用上面的 loadLanguageCodeFile 方法。任何有关如何调用静态方法的建议将不胜感激
你不应该嘲笑被测对象。您模拟被测对象的依赖项,这些依赖项是完成测试所需的。
该代码还与文件 reader 和缓冲区 reader 等实现问题紧密耦合。
然而,正如评论中所指出的,您想要在模拟设置提供的路径上测试文件的实际读取。
在那种情况下你只需要模拟 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
}
}