jbang icon indicating copy to clipboard operation
jbang copied to clipboard

@QuarkusTest is not working properly

Open nandorholozsnyak opened this issue 3 years ago • 3 comments
trafficstars

Describe the bug Testing Quarkus based JBang script is not working properly.

To Reproduce Steps to reproduce the behavior: Lets see the following code snippets:

Simple REST controller (RestEasyExample.java):

///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 11
//DEPS io.quarkus:quarkus-bom:2.6.0.Final@pom
//DEPS io.quarkus:quarkus-resteasy

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

public class RestEasyExample {

    @Path("/hello")
    public static class Controller {

        public Controller() {
        }

        @GET
        public Response hello() {
            return Response.ok("Hello World").build();
        }
    }

}

Test class for it (RestEasyExampleTest.java):


///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 11
//DEPS io.quarkus:quarkus-bom:2.6.0.Final@pom
//DEPS io.quarkus:quarkus-resteasy
//DEPS io.rest-assured:rest-assured
//DEPS io.quarkus:quarkus-junit5

//SOURCES RestEasyExample.java

import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.LoggingListener;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;

import static io.restassured.RestAssured.given;
import static java.lang.System.out;
import static org.hamcrest.Matchers.containsString;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;

@QuarkusTest //Quarkus Test typically using this annotation.
public class RestEasyExampleTest {

    @Test
    public void testCallingEndpoint() throws Exception {
        given()
                .contentType("application/json")
                .accept("application/json")
                .when()
                .get("/hello")
                .then()
                .statusCode(200)
                .body(containsString("Hello World"));
    }

    // It is probably not the way it should start the JUnit test with Quarkus but it was copied from this repository: https://github.com/grumpyf0x48/jbang-unit-tests/blob/main/FibonacciTest.java#L50
    public static void main(final String... args) {
        final LauncherDiscoveryRequest request =
                LauncherDiscoveryRequestBuilder.request()
                        .selectors(selectClass(RestEasyExampleTest.class))
                        .build();
        final Launcher launcher = LauncherFactory.create();
        final LoggingListener logListener = LoggingListener.forBiConsumer((t, m) -> {
            System.out.println(m.get());
            if (t != null) {
                t.printStackTrace();
            }
        });
        final SummaryGeneratingListener execListener = new SummaryGeneratingListener();
        launcher.registerTestExecutionListeners(execListener, logListener);
        launcher.execute(request);
        execListener.getSummary().printTo(new java.io.PrintWriter(out));
    }

}

After running the following command: jbang --verbose RestEasyExampleTest.java the result will be:

...
[jbang] [ERROR] Issue running postBuild()
dev.jbang.cli.ExitException: Issue running postBuild()
        at dev.jbang.spi.IntegrationManager.runIntegration(IntegrationManager.java:153)
        at dev.jbang.cli.BaseBuildCommand.buildJar(BaseBuildCommand.java:200)
        at dev.jbang.cli.BaseBuildCommand.build(BaseBuildCommand.java:131)
        at dev.jbang.cli.BaseBuildCommand.buildIfNeeded(BaseBuildCommand.java:95)
        at dev.jbang.cli.Run.prepareArtifacts(Run.java:100)
        at dev.jbang.cli.Run.doCall(Run.java:76)
        at dev.jbang.cli.BaseCommand.call(BaseCommand.java:93)
        at dev.jbang.cli.BaseCommand.call(BaseCommand.java:15)
        at picocli.CommandLine.executeUserObject(CommandLine.java:1953)
        at picocli.CommandLine.access$1300(CommandLine.java:145)
        at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2352)
        at picocli.CommandLine$RunLast.handle(CommandLine.java:2346)
        at dev.jbang.cli.JBang$3.handle(JBang.java:145)
        at dev.jbang.cli.JBang$3.handle(JBang.java:140)
        at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2179)
        at picocli.CommandLine.execute(CommandLine.java:2078)
        at dev.jbang.Main.main(Main.java:14)
Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at dev.jbang.spi.IntegrationManager.runIntegration(IntegrationManager.java:117)
        ... 16 more
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at io.quarkus.launcher.JBangIntegration.postBuild(JBangIntegration.java:128)
        ... 21 more
Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at io.quarkus.launcher.JBangIntegration.postBuild(JBangIntegration.java:120)
        ... 21 more
Caused by: java.lang.NoClassDefFoundError: org/codehaus/plexus/logging/AbstractLogEnabled
        at java.base/java.lang.ClassLoader.defineClass1(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
        at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
        at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:800)
        at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:698)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:621)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        at io.quarkus.bootstrap.resolver.maven.SecDispatcherImpl.<init>(SecDispatcherImpl.java:47)
        at io.quarkus.bootstrap.resolver.maven.SettingsDecrypterImpl.<init>(SettingsDecrypterImpl.java:21)
        at io.quarkus.bootstrap.resolver.maven.BootstrapMavenContext.newRepositorySystemSession(BootstrapMavenContext.java:431)
        at io.quarkus.bootstrap.resolver.maven.BootstrapMavenContext.getRepositorySystemSession(BootstrapMavenContext.java:256)
        at io.quarkus.bootstrap.resolver.maven.BootstrapMavenContext.resolveRemoteRepos(BootstrapMavenContext.java:511)
        at io.quarkus.bootstrap.resolver.maven.BootstrapMavenContext.getRemoteRepositories(BootstrapMavenContext.java:260)
        at io.quarkus.bootstrap.jbang.JBangBuilderImpl.postBuild(JBangBuilderImpl.java:34)
        ... 26 more
Caused by: java.lang.ClassNotFoundException: org.codehaus.plexus.logging.AbstractLogEnabled
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 43 more
[jbang] If you believe this a bug in jbang, open an issue at https://github.com/jbangdev/jbang/issues

This java.lang.ClassNotFoundException: org.codehaus.plexus.logging.AbstractLogEnabled class can be found in the following dependency: org.eclipse.sisu:org.eclipse.sisu.plexus but after adding it to the above script, the same happens.

Expected behavior Quarkus test should work in my opinion.

JBang version Paste output of jbang version --verbose

0.86.0

Additional context

nandorholozsnyak avatar Jan 02 '22 14:01 nandorholozsnyak

Not at computer ATM. Can you test if adding a package a package statement changes anything ?

In any case I think this is a quarkusio issue rather than jbang one!

maxandersen avatar Jan 02 '22 14:01 maxandersen

@maxandersen unfortunately still the same.

nandorholozsnyak avatar Jan 02 '22 14:01 nandorholozsnyak

Found a "workaround" but on the long term won't really work well, I was not able to test the Lambda functions but at least was able to test JAX-RS endpoints:

///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 11
//DEPS io.quarkus:quarkus-bom:2.6.0.Final@pom
//DEPS io.rest-assured:rest-assured
//DEPS org.junit.jupiter:junit-jupiter-api:5.7.2
//DEPS org.junit.jupiter:junit-jupiter-engine:5.7.2
//DEPS org.junit.platform:junit-platform-launcher:1.7.2
//DEPS commons-logging:commons-logging:1.2

//SOURCES RestEasyExample.java

import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.QuarkusApplication;
import io.quarkus.runtime.annotations.QuarkusMain;
import org.junit.jupiter.api.Test;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.LoggingListener;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;

import static io.restassured.RestAssured.given;
import static java.lang.System.out;
import static org.hamcrest.Matchers.containsString;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;

@QuarkusMain
public class RestEasyExampleTest implements QuarkusApplication {

    @Test
    public void testCallingEndpoint() throws Exception {
        given()
                .contentType("application/json")
                .accept("application/json")
                .when()
                .get("/hello")
                .then()
                .statusCode(200)
                .body(containsString("Hello World"));
    }

    public static void main(final String... args) {
        Quarkus.run(RestEasyExampleTest.class, args);
    }

    @Override
    public int run(String... args) throws Exception {
        final LauncherDiscoveryRequest request =
                LauncherDiscoveryRequestBuilder.request()
                        .selectors(selectClass(RestEasyExampleTest.class))
                        .build();
        final Launcher launcher = LauncherFactory.create();
        final LoggingListener logListener = LoggingListener.forBiConsumer((t, m) -> {
            System.out.println(m.get());
            if (t != null) {
                t.printStackTrace();
            }
        });
        final SummaryGeneratingListener execListener = new SummaryGeneratingListener();
        launcher.registerTestExecutionListeners(execListener, logListener);
        launcher.execute(request);
        execListener.getSummary().printTo(new java.io.PrintWriter(out));
        return 0;
    }
}

nandorholozsnyak avatar Jan 02 '22 16:01 nandorholozsnyak