sampig / spock

Automatically exported from code.google.com/p/spock
0 stars 0 forks source link

Spock and Slf4j combination causes compilation error #369

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
+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

GoogleCodeExporter commented 9 years ago
@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

GoogleCodeExporter commented 9 years ago
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