Spock: IllegalAccessError with swapInit and clinit

Created on 17 Sep 2018  路  19Comments  路  Source: spockframework/spock

We get this Error seemingly random places in our Spock tests after upgrading to Jacoco 0.8.2 in my build:

Caused by: java.lang.IllegalAccessError: Update to static final field me.SomeTest.$const$0 attempted from a different method (__$swapInit) than the initializer method <clinit>
    at me.SomeTest.__$swapInit(SomeTest.groovy)
    at me.SomeTest.<clinit>(SomeTest.groovy)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
    at java.base/java.lang.Class.newInstance(Class.java:560)
    at org.spockframework.runtime.BaseSpecRunner.createSpecInstance(BaseSpecRunner.java:104)

I'm using RC3.

I've also created an issue @ jacoco: https://github.com/jacoco/jacoco/issues/757

Most helpful comment

@saw303 Does it work if you replace 120L with 120 or (long) 120?

All 19 comments

(I was getting this error with 2.5.3-SNAPSHOT (https://github.com/spockframework/spock/pull/900#issuecomment-420158524). It seems to disappear after the latest snapshot got downloaded.)

@henrik242 please always post your build env, e.g. Java version, Spock Version, Groovy version

@leonard84 Groovy 2.5.3-SNAPSHOT and jdk 10.0.2 (I am also testing the latest jdk 11).

@martyix You might be correct with the groovy snapshot assumption, one team member got this error with jdk 10, while I could only reproduce it with 11. Maybe we're both good later today when we cash check a later snapshot.

We had similar problems when switching to JDK 11: Random tests failed with this error message.

We found a that in our case we could fix the failing test by changing all long and float primitives to ints.
For example the following line would cause the test to fail:
Id.of(1234L)
By changing it to the following the test would succeed:
Id.of(1234)

Similarly this fails:
1 * rs.getBigDecimal("id") >> 1.0
This does not:
1 * rs.getBigDecimal("id") >> 1

Obviously this is only a quick fix, but maybe someone has some insight into why this works?

I am getting this on Java 11 when working with java.time Objects such as Instant, LocalDate, LocalDateTime.

This simple test fails at runtime using Spock 1.2-groovy-2.5 and Groovy 2.5.3 on JDK 11.

void "Test something"() {
    expect:
    Instant.now().plusSeconds(120L) != null
}

It results in

Exception in thread "main" org.spockframework.util.InternalSpockError: Failed to instantiate spec 'APIKeyAuthenticationManagerSpec'
    at org.spockframework.runtime.BaseSpecRunner.createSpecInstance(BaseSpecRunner.java:110)
    at org.spockframework.runtime.BaseSpecRunner.run(BaseSpecRunner.java:62)
    at org.spockframework.runtime.Sputnik.run(Sputnik.java:63)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalAccessError: Update to static final field com.adcubum.gesus.offer.security.APIKeyAuthenticationManagerSpec.$const$0 attempted from a different method (__$swapInit) than the initializer method <clinit> 
    at com.adcubum.gesus.offer.security.APIKeyAuthenticationManagerSpec.__$swapInit(APIKeyAuthenticationManagerSpec.groovy)
    at com.adcubum.gesus.offer.security.APIKeyAuthenticationManagerSpec.<clinit>(APIKeyAuthenticationManagerSpec.groovy)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at java.base/java.lang.Class.newInstance(Class.java:584)
    at org.spockframework.runtime.BaseSpecRunner.createSpecInstance(BaseSpecRunner.java:104)
    ... 7 more

@saw303 https://github.com/spockframework/spock/pull/900#issuecomment-430760057 - this thread is about the same issue. Unfortunately, it is not clear, what causes the problem at the moment.

@saw303 Does it work if you replace 120L with 120 or (long) 120?

@henrik242 yes it works if I use 120 instead of 120L.

I have the same issue with java 11, Groovy 5.3, spock 1.2
All tests are running well in maven, but fails in Intellij IDEA when i put "project bytecode version" 11 or 10 in javac settings. If I put in IDEA "project bytecode version" to be 8, it is okay

Under Maven the exception is thrown either way - but for some reason surefire doesn't even mark the test as run - so failure is hidden. (surefire 2.22.1, other versions the same)

BigDecimals constants are also impacted (aka 1.2 ) , when changed to 1.2d (double) - the exception vanished.

I am using Java 10.0.2 (Zulu), Maven 3.6.0, Groovy 2.5.3, and GMavenPlus 1.6.2 (not even using Spock). I was getting the same error running Junit tests with Maven, even though they work fine in Eclipse. But when I changed all floating point literals to have a 'd' (e.g., "10.3" -> "10.3d") in my test class and all the classes it references, it started working in Maven. And it still works in Eclipse.

Just another data point as you try to figure this out. Now...my other project has about 20 classes throwing errors like this ;-o

There is already a provisional fix in groovy-2.5.4-SNAPSHOT

You mean as I was spending 4 hours trolling through all my code replacing "4.23" with "4.23d" and "45L" with "45", they had actually started the release vote at the same time?!?!? My anguish must have influenced them. Wahhhhhhhhhh....

Thanks for the heads-up tho.

Groovy 2.5.4 is out.

@henrik242 Fixed in #900

@henrik242 yes it works if I use 120 instead of 120L.
It helped me!
I use java 11 with spock and groovy.

This issue occurs with Groovy 2.5.7 (with spock-core:1.2-groovy-2.5 and 1.3-groovy-2.5):

java.lang.IllegalAccessError: Update to non-static final field MySpec.field attempted from a different method ($spock_initializeFields) than the initializer method <init>
    at MySpec.$spock_initializeFields(MySpec.groovy:##)

The test passes when Groovy 2.5.4 is used.

@jtstorck Looks like others have noticed that with Groovy 2.5.7 as well (see #963 after it was closed)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

woldie picture woldie  路  8Comments

lavcraft picture lavcraft  路  3Comments

szpak picture szpak  路  8Comments

ShunmugamAiyappan picture ShunmugamAiyappan  路  5Comments

Derbeth picture Derbeth  路  10Comments