【问题标题】:Using PowerMockito to mock Path.getParent()使用 PowerMockito 模拟 Path.getParent()
【发布时间】:2017-07-07 15:46:22
【问题描述】:

我收到以下错误: org.powermock.reflect.exceptions.MethodNotFoundException:在类 java.lang.Object 的类层次结构中找不到与名称 getParent 匹配的方法。

我不知道为什么。我尝试创建一些简单的代码来缩小错误范围,但不明白为什么这个模拟不起作用。

要测试的代码:

public void forTest(File xmlFile){
    Path p = Paths.get(xmlFile.getAbsolutePath());
    p.getParent();
}

测试代码:

@RunWith(PowerMockRunner.class)
@PrepareForTest({XmlFileFunctions.class,Paths.class})
public class XmlFileFunctionsTest {
    @InjectMocks
    XmlFileFunctions xmlFileFunctionsMock;

    @Mock
    File xmlFileMock;
    @Mock
    Path pMock;


    @Test
    public void myTest(){
        when(xmlFileMock.getAbsolutePath()).thenReturn("abc");
        PowerMockito.mockStatic(Paths.class);
        when(Paths.get(Matchers.anyString())).thenReturn(pMock);

        xmlFileFunctionsMock.forTest(xmlFileMock);
        verify(pMock).getParent();
    }

}

【问题讨论】:

    标签: java unit-testing junit4 powermockito


    【解决方案1】:

    走下不同的解决方案路径后,我找到了以下答案:Mockito and PowerMock MethodNotFoundException being thrown

    似乎将 powermock 的版本从 1.6.6 更改为 1.6.5 可以解决此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-30
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多