【问题标题】:Mocking static method using power mockito使用 powermockito 模拟静态方法
【发布时间】:2018-12-03 18:11:58
【问题描述】:

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

我正在尝试为此方法编写一个测试用例。 Systemsetting 是另一个类,

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 方法。任何关于如何调用静态方法的建议将不胜感激

【问题讨论】:

  • 你不应该模拟被测对象。您模拟完成测试所需的被测主题的依赖关系
  • 代码还与文件读取器和缓冲区读取器等实现问题紧密耦合。您打算在测试时读取实际文件吗?
  • 我同意@Nkosi;除了 IO 片段之外,我在这里没有看到任何可供您 to 模拟的东西。不过,它确实提出了一个强烈的问题,即FILENAME 的来源。
  • 你到底想用那种方法测试什么?
  • @LaxmiKadariya 调用被测的实际静态成员。 HashMap&lt;String, String&gt; result = Engine.loadLanguageCodeFile(hash_map);

标签: java junit mockito powermockito


【解决方案1】:

你不应该嘲笑被测对象。您模拟完成测试所需的被测主题的依赖关系。

代码还与文件读取器和缓冲区读取器等实现问题紧密耦合。

但是,如 cmets 所示,您希望在模拟设置提供的路径上测试文件的实际读取。

在这种情况下,您只需要模拟 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
    }
}

参考Using PowerMock with Mockito: Mocking Static Metho

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多