繁体   English   中英

使用power mockito模拟静态方法

[英]Mocking static method using power mockito

我有一个类Engine.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;
  }

我正在尝试为此方法编写测试用例。 系统设置是另一个类和

settings = SystemSettings.GetInstance();
      String path = settings.getLangCodePath();

`给出另一个类的实例,并在路径中包含路径文件,如\\ 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);
    }

我无法调用上面的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
    }
}

参考使用PowerMock和Mockito:模拟静态方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM