2017-11-11 4 views
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.에 업그레이드를 연기해야 ​​할 것 같아요 당신에게 데미안

답변

1

감사

PowerMockito not compatible Mockito2 since their v2.0.55-beta release을 참조하십시오.

모든 PowerMock의 버전 2.x/Mockito 버전 2.x 통합 작업은이 두 가지 문제로 덮여있다 :

그것은 이 같은 보이는 목표는 PowerMock v2.0.0 (및 일부 Mockito 2.x 버전)에서이 작업을 수행하는 것이지만 사용할 수있는시기에 대한 명확한 진술은 없습니다.

+0

완벽한 - 감사합니다. @glytghing – Damien