Spock and Slf4j combination causes compilation error
GoogleCodeExporter opened this issue · comments
Google Code Exporter commented
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.
Original issue reported on code.google.com by msa...@gmail.com
on 9 Jul 2014 at 12:52
Google Code Exporter commented
Just FYI this happens with @log4j and presumably also the other logging
transforms.
Original comment by jamie.ec...@gmail.com
on 6 Aug 2014 at 5:02
Google Code Exporter commented
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.
Original comment by pnied...@gmail.com
on 6 Aug 2014 at 5:28
Google Code Exporter commented
+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
Original comment by belov...@gmail.com
on 17 Nov 2014 at 11:14
Google Code Exporter commented
@belov.fa That sounds like a different problem. Can you provide more details?
Original comment by pnied...@gmail.com
on 17 Nov 2014 at 12:15
Google Code Exporter commented
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.
Original comment by fracture...@gmail.com
on 9 Jun 2015 at 9:27