1
은 다음 코드는 2.9.0Mockito 및 PowerMockito 오류
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 버전 1.7.3 및 Mockito 버전과 함께 작동 - 내가 갈 때 다음 버전 - 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
이 문제 또는 내가 무엇을 변경해야하는 원인이 될 수 무엇을 어떤 아이디어를 얻을?
난 당신이/다운 그레이드 PowerMock의 v2.x.에 업그레이드를 연기해야 할 것 같아요 당신에게 데미안
완벽한 - 감사합니다. @glytghing – Damien