I have a test that used to run fine in PowerMockito 1.7.1, since moving to java 9 and 2.0.0-beta.5 I am seeing issues with the same test.
public class StaticVerifyTestClass {
public Path doPathFileStuff(Path path) {
try {
return Files.createDirectories(path);
} catch (IOException e) {
throw new RuntimeException("It broke", e);
Example test:
@PrepareForTest({ Files.class, StaticVerifyTestClass.class })
public class PowerMockitoStaticVerify {
@Rule
public PowerMockRule rule = new PowerMockRule();
@Test
public void createLogDumpDirectoriesFileExists() throws Exception {
Path mockPath = mock(Path.class);
mockStatic(Files.class);
when(Files.createDirectories(eq(mockPath))).thenReturn(mockPath);
new StaticVerifyTestClass().doPathFileStuff(mockPath);
verifyStatic(Files.class);
Files.createDirectories(eq(mockPath));
I am seeing the following error on running the test:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/Users/david.birch/.gradle/caches/modules-2/files-2.1/com.thoughtworks.xstream/xstream/1.4.10/dfecae23647abc9d9fd0416629a4213a3882b101/xstream-1.4.10.jar) to field java.util.TreeMap.comparator
WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Security framework of XStream not initialized, XStream is probably vulnerable.
org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class java.nio.file.Files
Mockito cannot mock/spy because :
- final class
I know that 2.0.0-beta.5 resolved a lot of these, did one slip through the cracks or has the approach changed for these kind of mocks between the two versions?
JDK9, power mockito 2.0.0-beta.5 - final class issue when mocking Files
JDK9, power mockito 2.0.0-beta.5 - final class issue when mocking static method
Feb 23, 2018
@thekingnothing - The PR which listed as fixing this was, in fact, fixing a different problem. It was resolving the line, from above:
Security framework of XStream not initialized, XStream is probably vulnerable.
However, the title clearly states that the issue is the
org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class java.nio.file.Files
Mockito cannot mock/spy because :
- final class
Can this bug be reopened? I'm still encountering this with
<mockito.version>2.28.2</mockito.version>
<powermock.version>2.0.2</powermock.version>
Update - I've just identified it's a combination of powermock 2.x with mockito 2.27.0 (and newer).
The same failing tests work fine under pm 2.0.2 and mockito 2.26.0