使用Power Mockito模拟静态方法

时间:2018-12-03 18:11:58

标签: java junit mockito powermockito

我有一个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();

`给出另一个类的实例,并在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);
    }

我无法调用上述loadLanguageCodeFile方法。任何建议如何调用静态方法将不胜感激

1 个答案:

答案 0 :(得分:3)

您不应嘲笑被测对象。您可以模拟完成测试所需的被测对象的依存关系。

代码还与诸如文件读取器和缓冲区读取器之类的实现问题紧密联系在一起。

但是,如注释中所示,您想要在模拟设置提供的路径下测试文件的实际读取。

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

相关问题