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

Deencapsulation.setField() is broken for JDK8 after I upgrade to v1.24 #281

Closed
pmodulo opened this Issue May 30, 2016 · 2 comments

Comments

2 participants
@pmodulo

pmodulo commented May 30, 2016

Please provide the following information:

  • Version of JMockit that was used: v1.24
  • Description of the problem or enhancement request:Deencapsulation.setField() is broken for JDK8 after I upgrade to v1.24. My tests started to fail with the following error message:

java.lang.RuntimeException: java.lang.IllegalAccessException: Can not set static final java.util.HashMap field xxx.xxx.xxx to java.util.HashMap
Caused by:
java.lang.IllegalAccessException: Can not set static final java.util.HashMap field xxx.xxx.xxx to java.util.HashMap
at java.lang.reflect.Field.set(Field.java:764)
... 3 more

@rliesenfeld

This comment has been minimized.

Show comment
Hide comment
@rliesenfeld

rliesenfeld May 30, 2016

Member

I am unable to reproduce the problem. Could you show an example failing test?

Member

rliesenfeld commented May 30, 2016

I am unable to reproduce the problem. Could you show an example failing test?

@pmodulo

This comment has been minimized.

Show comment
Hide comment
@pmodulo

pmodulo May 30, 2016

import java.util.HashMap;

public class MyClass {
private static final int SOME_CONSTANT = 12;

private static final HashMap map = new HashMap();
}

import org.junit.Test;

import mockit.Deencapsulation;

import static org.junit.Assert.assertEquals;

public class MyClassTest {
@test
public void jMockitBug() {
MyClass clz = new MyClass();
int orig = Deencapsulation.getField(MyClass.class, "SOME_CONSTANT");
Deencapsulation.setField(MyClass.class, "SOME_CONSTANT", 20);
int updated = Deencapsulation.getField(MyClass.class, "SOME_CONSTANT");
assertEquals(20, updated);
Deencapsulation.setField(MyClass.class, "SOME_CONSTANT", orig);
assertEquals(orig, (int) Deencapsulation.getField(MyClass.class, "SOME_CONSTANT"));
}
}

There was 1 failure:

  1. jMockitBug(MyClassTest)
    java.lang.RuntimeException: java.lang.IllegalAccessException: Can not set static final int field MyClass.SOME_CONSTANT to java.lang.Integer
    at MyClassTest.jMockitBug(MyClassTest.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:498)
    Caused by: java.lang.IllegalAccessException: Can not set static final int field MyClass.SOME_CONSTANT to java.lang.Integer
    at java.lang.reflect.Field.set(Field.java:764)
    ... 3 more

FAILURES!!!
Tests run: 1, Failures: 1

java -version
java version "1.8.0_74"
Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)

pmodulo commented May 30, 2016

import java.util.HashMap;

public class MyClass {
private static final int SOME_CONSTANT = 12;

private static final HashMap map = new HashMap();
}

import org.junit.Test;

import mockit.Deencapsulation;

import static org.junit.Assert.assertEquals;

public class MyClassTest {
@test
public void jMockitBug() {
MyClass clz = new MyClass();
int orig = Deencapsulation.getField(MyClass.class, "SOME_CONSTANT");
Deencapsulation.setField(MyClass.class, "SOME_CONSTANT", 20);
int updated = Deencapsulation.getField(MyClass.class, "SOME_CONSTANT");
assertEquals(20, updated);
Deencapsulation.setField(MyClass.class, "SOME_CONSTANT", orig);
assertEquals(orig, (int) Deencapsulation.getField(MyClass.class, "SOME_CONSTANT"));
}
}

There was 1 failure:

  1. jMockitBug(MyClassTest)
    java.lang.RuntimeException: java.lang.IllegalAccessException: Can not set static final int field MyClass.SOME_CONSTANT to java.lang.Integer
    at MyClassTest.jMockitBug(MyClassTest.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:498)
    Caused by: java.lang.IllegalAccessException: Can not set static final int field MyClass.SOME_CONSTANT to java.lang.Integer
    at java.lang.reflect.Field.set(Field.java:764)
    ... 3 more

FAILURES!!!
Tests run: 1, Failures: 1

java -version
java version "1.8.0_74"
Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)

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