Originally reported on Google Code with ID 369
When using Spock in combination with Slf4j, compilation errors can occur.
Example test with compilation error:
-------------
@Slf4j
class ExampleUnitTest extends Specification {
def 'exercise compilation error'() {
given:
true
when:
true
then:
[1, 2, 3].each { assert true }
}
}
----------------
Resulting error:
----------------
Error:Groovyc: The current scope already contains a variable of the name $spock_valueRecorder
--------------------
Doing any of the following will fix the test:
- comment out the '@Slf4j' line
- remove the 'assert' from within the closure
- move the 'assert' outside the closure
Again, this is a combination of having an 'assert' inside a closure, while also having
the test annotated with @Slf4j.
This occurs with:
spock version 0.7-groovy-2.0
groovy version = 2.3.3-indy
This *does not* occur with:
spock version 0.7-groovy-2.0
groovy version = 2.3.1-indy
We tend to log in our unit and integration tests, and Slf4j has worked great so far
with us. However, we are now stuck at either Groovy 2.3.1, or need to remove all instances
of log messages. Removing assertions from closures is probably not an option, since
it would invalidate many tests.
Reported by msal98 on 2014-07-09 12:52:17
Just FYI this happens with @log4j and presumably also the other logging transforms.
Reported by jamie.echlin on 2014-08-06 17:02:38
For Groovy 2.3, 1.0-groovy-2.3-SNAPSHOT should be used. Can't say if it will solve this
particular problem though. Perhaps best not to use logging transforms inside Spock
specs.
Reported by pniederw on 2014-08-06 17:28:58
+1. Doesn't work with spock 1.0-groovy-2.3-SNAPSHOT and groovy-all 2.3.6.
Fixed by switching to spock 0.7-groovy-2.0 and groovy-all 2.3.1
I don't have @log4j. Only spring test configuration annotations
Reported by belov.fa on 2014-11-17 11:14:02
@belov.fa That sounds like a different problem. Can you provide more details?
Reported by pniederw on 2014-11-17 12:15:56
I'm using spock-1.0-groovy24 and Groovy 2.4.3, and the OP's issue is happening for me
as well. I'm currently working around it by extending all specs from a base class which
imports org.slf4j.LoggerFactory and defines @Shared log = LoggerFactory.getLogger(this.getClass()).
The annotation would be more straightforward and help to keep things consistent across
our Groovy stack though.
Reported by fracturetrey on 2015-06-09 09:27:20
Hi,
I am using spock-core:1.0-groovy-2.4. I am also facing same error:
/Users/pranav_gupta/plugins/calypso-tdd-plugin/branches/CALYPSO-TDD-PLUGIN_2-0_PATCHES_GRADLE2/src/test/groovy/com/calypso/gradle/tdd/integrationtest/taskchaining/AbstractRunChainedTask.groovy: -1: The current scope already contains a variable of the name $spock_valueRecorder
@ line -1, column -1.
Can somebody help me?
I am also seeing this. I just upgraded to...
org.codehaus.groovy:groovy-all:jar:2.4.3
org.spockframework:spock-core:jar:1.0-groovy-2.4
removing @Slf4j annotation from the child or parent eliminates the problem but a fix would be nice.
Also see this bug using the following spock versions together with Spring Boot 1.4.3.RELEASE
testCompile("org.spockframework:spock-core:1.1-groovy-2.4-rc-2")
testCompile("org.spockframework:spock-spring:1.1-groovy-2.4-rc-2")
The test class inherited from a parent class annotated with @Slf4j, which, however, doesn't affect subclasses.
Btw, I'm surprised that this bug hasn't been addressed yet...
My guess is that the AST modifications (log and spock) interfere with each other. I can only say that this is a fairly low priority, as using logging in tests is not that common/recommended. If there is an easy fix we will accept a PR for this.
Workaround` is to declare the logger in the non-Groovy, plain-ol'-Java way:
//@Slf4j
class TestClass extends Spec {
final static Logger log = LoggerFactory.getLogger(TestClass.class)
This currently appears to work with 1.1-groovy-2.4.
@JeffQuandt Not working with 1.1-groovy-2.4.
Just got hit by this issue with following versions (using @Slf4j):
org.spockframework:spock-spring:1.1-groovy-2.4
org.spockframework:spock-core:1.1-groovy-2.4
Fixed with #856
Is this fix available in any of the master releases for Spock yet? I have some closures that perform assertions within some Specs (I do not use @Log or @Slf4j annotations). I recently upgraded from Grails 2.3.11 to latest 3.3.8, and these Spock errors appear in only the files in which i use assertions within closures. Great find, @robfletcher.
I see that the fix from #856 was added to 1.2-RC2-groovy-2.5, however Grails 3.3 uses Groovy version 2.4.15, and Spock version 1.1-groovy-2.4. Is there a version of this RC2 that is compatible with groovy 2.4?
@gantaa as stated in the readme
The latest release version is 1.2-RC2 (1.2-RC2-groovy-2.4, 1.2-RC2-groovy-2.5), released on 2018-08-14.
My mistake. thanks for the quick reply. I was mis-led because I didnt see the 1.2-RC2-groovy-2.4 listed in maven repo browser: https://mvnrepository.com/artifact/org.spockframework/spock-core
But after adding the dependency anyways it downloads fine. thank you
1.2-RC2-groovy-2.4 also fixes the same issue I had with using @Memoized transformation on helper methods inside spec
Most helpful comment
Workaround` is to declare the logger in the non-Groovy, plain-ol'-Java way: