Testunit with @MeecrowaveConfig fail

63 Views Asked by At

I wrote a simple test, like the code here, that alone runs correctly. But if I try to write a second test using the same annotation @MeecrowaveConfig I have an error.

In other words I cannot have two class with the @MeecrowaveConfig annotation, because when the second Meecrowave instance starts the test fail with:

SEVERE: An error occurred while starting application context path : []
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.558 s <<< FAILURE! - in it.freedev.Test1
[ERROR] it.freedev.Test1  Time elapsed: 1.558 s  <<< ERROR!
java.lang.IllegalStateException: Error starting child
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].[]]
Caused by: org.apache.webbeans.exception.DuplicateDefinitionException: PassivationCapable bean id is not unique: SERVLET_CONTEXT#interface javax.servlet.ServletContext#@javax.enterprise.inject.Default(),@javax.enterprise.inject.Any(), bean:ServletContext, WebBeansType:SERVLET_CONTEXT, Name:null, API Types:[java.lang.Object,javax.servlet.ServletContext], Qualifiers:[javax.enterprise.inject.Default,javax.enterprise.inject.Any], existing: ServletContext, WebBeansType:SERVLET_CONTEXT, Name:null, API Types:[java.lang.Object,javax.servlet.ServletContext], Qualifiers:[javax.enterprise.inject.Default,javax.enterprise.inject.Any]

As said if I copy this class into a new class Test2, when I try to run all the tests the second fails with the error reported above.

@MeecrowaveConfig(scanningPackageIncludes="it.lux.ml4c.catalog")
public class Test1 {

    @ConfigurationInject
    public Meecrowave.Builder config;

    private static HttpClient client;

    @BeforeAll
    public static void init() {
        client = HttpClient.newBuilder()
                           .version(HttpClient.Version.HTTP_2)  // this is the default
                           .build();
    }

    @Test
    public void whenOpenApiCalls_thenIsAvailable() throws IOException, InterruptedException {
        final HttpRequest request = HttpRequest.newBuilder()
                                               .uri(URI.create("http://localhost:" + config.getHttpPort() + "/api/v1/openapi"))
                                               .GET().build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        assertEquals(200, response.statusCode());
    }
}
2

There are 2 best solutions below

0
freedev On

After many tries I find out that tomcatWrapLoader annotation parameter helps to avoid the problem.

@MeecrowaveConfig(scanningPackageIncludes="it.lux.ml4c.catalog", tomcatWrapLoader=true)
0
Romain Manni-Bucau On

Maybe check your classpath Seems a cdi extension adds a servletcontext bean already provided by meecrowave. Disabling that bean would solve it.