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

Closed
dbirch-cognitoiq opened this Issue Feb 23, 2018 · 2 comments

Comments

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

@thekingnothing

This comment has been minimized.

Copy link
Member

thekingnothing commented Mar 12, 2018

@LeandroRebelo

This comment has been minimized.

Copy link

LeandroRebelo commented Nov 20, 2018

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

timomeinen added a commit to timomeinen/powermock that referenced this issue Nov 30, 2018

Fixes powermock#888: Disable Security of X-Stream
JDK9, power mockito 2.0.0-beta.5 -
final class issue when mocking static method powermock#888

Unconfigured security in X-Stream leads to error message during build:
"Security framework of XStream not initialized,
XStream is probably vulnerable."

This commit will configure XStream security by allowing all types.

@thekingnothing thekingnothing added the bug label Dec 25, 2018

@thekingnothing thekingnothing added this to To Do in PowerMock 2.x via automation Dec 25, 2018

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