Git Product home page Git Product logo

Comments (2)

SanderSmee avatar SanderSmee commented on August 17, 2024 1

Thank you for looking into this, and the effort in trying to solve it in LogCaptor.
In my own investigation I did see where the exception was thrown, and that the classes were from different classloaders. Your conclusions match the suspicion I had at that point: it's a quarkus thing that is not easily solved. Further research shows that @QuarkusTest is known to do advanced things with classloading, and also disrupts usage of other junit5 extensions (e.g. junit-kafka).

Also thanks for the suggestion of ConsoleCaptor. I'll check how I can make that work in my current situation.

from log-captor.

Hakky54 avatar Hakky54 commented on August 17, 2024

Hoi Sander,

Gaaf om een Nederlandse ontwikkelaar hier te zien, welkom!

Thank you for raising this issue. The sample project is very helpful to easily reproduce the issue. The exception message you are getting is confusing and should be improved. I will create a change in the library for that specific use case.

LogCaptor needs Logback to capture logs and in your case it is already using that so it should not raise the exception, but this exception is being caused because of a different reason. The Logger of GreetingResource is created by jdk.internal.loader.ClassLoaders$AppClassLoader while all classes within the test even LogCaptor is created by io.quarkus.bootstrap.classloading.QuarkusClassLoader because of the @QuarkusTest annotation. It will try to cast the captured Logger which is of the type ch.qos.logback.classic.Logger to ch.qos.logback.classic.Logger but it fails because those to objects are totally different classes to the JVM because they are created by different classloaders.

I have tried to resolve this issue on the library side by changing the classloader order, and also used reflection to specify which classloader the library should use and some other methods but I could not resolve it. I would advise to create the same issue at https://github.com/quarkusio/quarkus They might know how to resolve this classloader issue as the annotation @QuarkusTest is causing this issue.

For the time being you can use ConsoleCaptor, which is also created by me. That library captures everything from the console output. If you make sure to forward all of your logs to the console by using slf4j-simple than console captor can easily capture those logs. You can enable it by adding the following imports to your build.gradle file:

testImplementation('io.github.hakky54:consolecaptor:1.0.2')
testImplementation('ch.qos.logback:logback-classic:1.2.11')
@Path("/hello")
public class GreetingResource {
    Logger logger = LoggerFactory.getLogger(GreetingResource.class);

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        logger.info("Hello");

        return "Hello RESTEasy";
    }
}
import io.quarkus.test.junit.QuarkusTest;
import nl.altindag.console.ConsoleCaptor;
import org.acme.GreetingResource;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@QuarkusTest
class QuarkusTestTest {

    @Test
    void captureLogs() {
        try(ConsoleCaptor consoleCaptor = new ConsoleCaptor()) {
            new GreetingResource().hello();

            List<String> standardOutput = consoleCaptor.getStandardOutput();

            assertEquals(1, standardOutput.size());
            assertTrue(standardOutput.get(0).contains("Hello"));
        }
    }

}

And the following properties file in the test resource: logback-test.xml See below for the file content

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="TRACE">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

So my advise is to raise this issue at Quarkus and in the mean while you can try using ConsoleCaptor till Quarkus can provide a solution. Hope this alternative solution is something which is useful for you.

from log-captor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.