【发布时间】: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