5

I'm trying to test the behavior of a logger with JUnit and SLF4J Test, which is "a test implementation of SLF4J that stores log messages in memory and provides methods for retrieving them".

From the SLF4J Test docs:

SLF4J Test should be the only SLF4J implementation on your test classpath

I have a few dependencies that have SLF4J as a transitive dependency. I'm trying to exclude SLF4J from all dependencies in my test configuration, but I still need it for SLF4J Test.

I can exclude SLF4J from everything with the below code, but this obviously also excludes it from SLF4J Test, where I need it.

configurations {
    testCompile.exclude group: "org.slf4j"
}

As SLF4J is a transitive dependency of many of my other dependencies, including Spring Boot, it isn't practical (or possible?) to go through and individually exclude it from all of them.

Is there a (relatively painless) way I can exclude a transitive dependency from all dependencies except the one that needs it?

chrisxrobertson
  • 745
  • 6
  • 9

1 Answers1

2

SLF4J Test guide says that you should have only one SLF4J implementation on your test classpath. It means that you should add slf4j-test dependency and exclude other ones (like, logback, log4j and etc.) only in the current project.

For example, Spring Boot uses Logback by default, here is snippet of gradle script describing how to use slf4j-test:

configurations.testCompile {
    exclude group: 'ch.qos.logback', module: 'logback-classic'
}

dependencies {
    compile(group: 'org.springframework.boot', name: 'spring-boot-starter')
    ...

    testCompile(group: 'org.springframework.boot', name: 'spring-boot-starter-test')
    testCompile(group: 'com.github.valfirst', name: 'slf4j-test', version: '1.3.0')
}
VaL
  • 1,128
  • 15
  • 29