Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDK9, power mockito 2.0.0-beta.5 - final class issue when mocking static method #888

Open
dbirch-cognitoiq opened this issue Feb 23, 2018 · 7 comments · May be fixed by marcus-nl/camunda-bpm-platform#7 or marcus-nl/camunda-bpm-platform#9
Labels

Comments

@dbirch-cognitoiq
Copy link

dbirch-cognitoiq commented Feb 23, 2018

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.

I have a reproduction scenario:
Example class:

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?

@dbirch-cognitoiq dbirch-cognitoiq changed the title 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
@thekingn0thing
Copy link
Member

Please, try powermock-2.0.0-beta.7

@leandrorebelo
Copy link

I'm tried the 2.0.0-RC.4 version, but, there is the same problem 👎

@ashulinskiy
Copy link

Hi,

have the exact same problem, is there an ETA for the solution please?

Thanks,
Andrey.

@elisman
Copy link

elisman commented Mar 11, 2019

Same here. This bug doesn't seem fixed.

Also, shouldn't powermock-classloading-objenesis have the same problem? There seems to only exist a patch for powermock-classloading-xstream. Anyway, I still get the error using any of the classloading modules.

@andrewring
Copy link

@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>

@adamh128
Copy link

I'm also seeing the same issue with JDK 8, mockito 2.82.2 and powermock 2.0.2.

@adamh128
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment