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

Closed
prashant999 opened this Issue May 30, 2016 · 5 comments

Projects

None yet

3 participants

@prashant999

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

@prashant999

Same tests worked fine with JMockit v1.20 and JMockit v1.23

@rliesenfeld
Member

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

@prashant999

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)

@rliesenfeld rliesenfeld added bug and removed could not reproduce labels May 30, 2016
@rliesenfeld rliesenfeld self-assigned this May 30, 2016
@rliesenfeld rliesenfeld added a commit that closed this issue Jun 4, 2016
@rliesenfeld rliesenfeld Dropped the support for setting static final fields through the Deenc…
…apsulation API, since it cannot be implemented in a way that reliably works for all versions of Java and in all usage scenarios; closes #281.
3819065
@rliesenfeld rliesenfeld added other and removed bug labels Jun 4, 2016
@Vampire
Vampire commented Jun 28, 2016

Why did you drop that support? This was essential for some of our tests and it worked fine up to 1.24. :-(

@Vampire
Vampire commented Jul 4, 2016

Ok, I updated our tests to use a MockUp instead. This is cleaner anyway.

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