使用 PowerMockito 模拟 Path.getParent()
Using PowerMockito to mock Path.getParent()
我收到以下错误:
org.powermock.reflect.exceptions.MethodNotFoundException:在 class java.lang.Object 的 class 层次结构中找不到与名称 getParent 匹配的方法 java.lang.Object。
我不知道为什么。我已经尝试创建一些简单的代码来缩小错误范围,但不明白为什么这个模拟不起作用。
要测试的代码:
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();
}
}
在走上不同的解决方案路径后,我找到了以下答案:
似乎将 powermock 的版本从 1.6.6 更改为 1.6.5 可以解决此问题。
我收到以下错误: org.powermock.reflect.exceptions.MethodNotFoundException:在 class java.lang.Object 的 class 层次结构中找不到与名称 getParent 匹配的方法 java.lang.Object。
我不知道为什么。我已经尝试创建一些简单的代码来缩小错误范围,但不明白为什么这个模拟不起作用。
要测试的代码:
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();
}
}
在走上不同的解决方案路径后,我找到了以下答案:
似乎将 powermock 的版本从 1.6.6 更改为 1.6.5 可以解决此问题。