Java Mockito和PowerMockito错误
以下代码适用于PowerMockito版本1.7.3和Mockito版本2.9.0Java Mockito和PowerMockito错误,java,junit,mocking,mockito,powermockito,Java,Junit,Mocking,Mockito,Powermockito,以下代码适用于PowerMockito版本1.7.3和Mockito版本2.9.0 import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import org.junit.Test; import org.
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({FileUtils.class, Paths.class, Files.class})
public class FileUtilsTest {
@Test
public void testGetFileContents_Success() throws Exception {
String filePath = "c:\\temp\\file.txt";
Path mockPath = PowerMockito.mock(Path.class);
PowerMockito.mockStatic(Paths.class);
PowerMockito.mockStatic(Files.class);
Mockito.when(Paths.get(Mockito.anyString())).thenReturn(mockPath);
Mockito.when(Files.readAllBytes(Mockito.isA(Path.class))).thenReturn("hello".getBytes());
String fileContents = FileUtils.getFileContents(filePath);
assertNotNull(fileContents);
assertTrue(fileContents.length() > 0);
PowerMockito.verifyStatic(Paths.class);
Paths.get(Mockito.anyString());
PowerMockito.verifyStatic(Files.class);
Files.readAllBytes(Mockito.isA(Path.class));
}
}
然而,当我转到以下版本时——PowerMockito版本2.0.0-beta.5和Mockito版本2.12.0——我得到了以下错误
org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class java.nio.file.Paths
Mockito cannot mock/spy because :
- final class
你知道是什么导致了这个问题,或者我需要改变什么吗
多谢各位
Damien我认为您必须降级/推迟到PowerMock v2.x的升级 看 以下两个问题涵盖了所有PowerMock v2.x/Mockito v2.x集成工作:
- PowerMock:
- 莫基托: