title: Unit testing of a class with StaticLoggerBinder
body: <p>I do have a simple class like this:</p>
  
  <pre><code>package com.example.howtomocktest
  
  import groovy.util.logging.Slf4j
  import java.nio.channels.NotYetBoundException
  
  @Slf4j
  class ErrorLogger {
      static void handleExceptions(Closure closure) {
          try {
              closure()
          }catch (UnsupportedOperationException|NotYetBoundException ex) {
              log.error ex.message
          } catch (Exception ex) {
              log.error 'Processing exception {}', ex
          }
      }
  }
  </code></pre>
  
  <p>And I would like to write a test for it, here is a skeleton:</p>
  
  <pre><code>package com.example.howtomocktest
  
  import org.slf4j.Logger
  import spock.lang.Specification
  import java.nio.channels.NotYetBoundException
  import static com.example.howtomocktest.ErrorLogger.handleExceptions
  
  class ErrorLoggerSpec extends Specification {
  
     private static final UNSUPPORTED_EXCEPTION = { throw UnsupportedOperationException }
     private static final NOT_YET_BOUND = { throw NotYetBoundException }
     private static final STANDARD_EXCEPTION = { throw Exception }
     private Logger logger = Mock(Logger.class)
     def setup() {
  
     }
  
     def "Message logged when UnsupportedOperationException is thrown"() {
        when:
        handleExceptions {UNSUPPORTED_EXCEPTION}
  
        then:
        notThrown(UnsupportedOperationException)
        1 * logger.error(_ as String) // doesn't work
     }
  
     def "Message logged when NotYetBoundException is thrown"() {
        when:
        handleExceptions {NOT_YET_BOUND}
  
        then:
        notThrown(NotYetBoundException)
        1 * logger.error(_ as String) // doesn't work
     }
  
     def "Message about processing exception is logged when standard Exception is thrown"() {
        when:
        handleExceptions {STANDARD_EXCEPTION}
  
        then:
        notThrown(STANDARD_EXCEPTION)
        1 * logger.error(_ as String) // doesn't work
     }
  }
  </code></pre>
  
  <p>The logger in ErrorLogger class is provided by StaticLoggerBinder, so my question is - how do I make it work so that those checks "1 * logger.error(_ as String)" would work? I can't find a proper way of mocking that logger inside of ErrorLogger class. I have thought about reflection and somehow accessing it, furthermore there was an idea with  mockito injection (but how to do that if reference to an object is not even present in that class because of that Slf4j annotation!) Thanks in advance for all your feedback and advices.</p>
  
  <p>EDIT: Here is an output of a test, even 1*logger.error(_) doesn't work.</p>
  
  <pre><code>Too few invocations for:
  
  1*logger.error()   (0 invocations)
  
  Unmatched invocations (ordered by similarity):
  </code></pre>