spock: Spock and Slf4j combination causes compilation error

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

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 2
  • Comments: 18 (9 by maintainers)

Most upvoted comments

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)

@JeffQuandt Not working with 1.1-groovy-2.4.