18

I set up a small test project using java 9 modules. The structure looks like this:

.
├── build.gradle
└── src
    ├── main
    │   └── java
    │       ├── module-info.java
    │       └── slfTest
    │           └── Main.java
    └── test
        └── java
            └── slfTest
                └── MainTest.java

(Feel free to clone and have a look yourself: git clone https://github.com/michas2/slfTest.git)

The classes Main and Main Test only log some simple output:

    Logger logger = LoggerFactory.getLogger(Main.class);
    logger.info("Hello World");

Now gradle run works as expected, but gradle test gives a ClassCastException.

    $ gradle run -q
    [main] INFO slfTest.Main - Hello World
    $ gradle test -q
    java.lang.ClassCastException: org.slf4j.simple/org.slf4j.simple.SimpleLoggerFactory cannot be cast to org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext
            at org.gradle.internal.logging.slf4j.Slf4jLoggingConfigurer.configure(Slf4jLoggingConfigurer.java:42)
            at org.gradle.internal.logging.config.LoggingSystemAdapter.startCapture(LoggingSystemAdapter.java:54)
            at org.gradle.internal.logging.services.DefaultLoggingManager$StartableLoggingSystem.start(DefaultLoggingManager.java:297)
            at org.gradle.internal.logging.services.DefaultLoggingManager.start(DefaultLoggingManager.java:73)
            at org.gradle.internal.logging.services.DefaultLoggingManager.start(DefaultLoggingManager.java:37)
            at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:83)
            at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:64)
            at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:62)
            at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:67)

Removing the java 9 module makes things work again. Therefore I assume there is module access problem. - What is the right way to fix it?


The content of module-info.java is:

module slfTest {
    requires  org.slf4j;
    exports slfTest;
}

When gradle runs the tests, gradle tries to inject its own logging backend. - Guess this is the part where some module access problem arise.

michas
  • 25,361
  • 15
  • 76
  • 121
  • 1
    I might be asking a bit more, but since you are anyway looking for a solution, could you ensure and share the details of the version of `gradle` and `slf4j` in use being compatible with Java9? – Naman Apr 09 '18 at 06:24
  • I used the latest gradle (4.6) and also played a bit with different version combinations. Building and running the application itself works fine, only running tests raises the issue. – michas Apr 09 '18 at 07:01
  • This seems to be a gradle bug: https://github.com/gradle/gradle/issues/2657. This [related issue](https://github.com/gradle/gradle/issues/4981) provides the reasoning. – sujit Apr 12 '18 at 12:21
  • @sujit perhaps that should be an Answer? – Jolta Sep 19 '18 at 12:02

1 Answers1

1

This is a known issue in gradle.

For more reference see here at

  1. Depending on slf4j causes test worker throw ClassCastException
  2. SLF4J ClassCastException when testing with custom system ClassLoader or Java 9+
Akhil Jain
  • 13,872
  • 15
  • 57
  • 93