'Micronaut context configuration for test

I'm trying to write a basic controller test in a micronaut (3.2.7) application. When I run it, it fails to start as it wants to create DB related beans too. micronaut-hibernate-jpa, flyway, etc. are in the pom.xml.

Can I configure the context somehow so it doesn't pick up hikaripool,flyway, and jpa realted beans?

11:46:23.820 [main] INFO  i.m.context.env.DefaultEnvironment - Established active environments: [test]
11:46:24.112 [main] WARN  i.m.c.h.j.JpaConfiguration$EntityScanConfiguration - Runtime classpath scanning is no longer supported. Use @Introspected to declare the packages you want to index at build time. Example @Introspected(packages="foo.bar", includedAnnotations=Entity.class)
11:46:24.133 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
11:46:25.197 [main] ERROR com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"

The code:

class HelloTest {

    private static EmbeddedServer server;
    private static HttpClient client;

    @BeforeAll
    public static void setupServer() {
        server = ApplicationContext.run(EmbeddedServer.class);
        client = server
            .getApplicationContext()
            .createBean(HttpClient.class, server.getURL());
    }

    @AfterAll
    public static void stopServer() {
        if (server != null) {
            server.stop();
        }
        if (client != null) {
            client.stop();
        }
    }

    @Test
    void testHelloWorldResponse() {
      ...
    }

}

I tried to exclude configurations like this, but with no luck:

        server = ApplicationContext.builder("test")
            .exclude("io.micronaut.configuration.hibernate.jpa","io.micronaut.configuration.jdbc.hikari")
            .run(EmbeddedServer.class);

Note: If I remove everything from application.yml then the test works. It looks like that in tests the default properties are resolved which turns on jpa,metrics, etc. So I guess the test needs to ignore the default settings too somehow.



Solution 1:[1]

You can override all of your (default) application.yml with (test-)environment specific property files: https://docs.micronaut.io/latest/guide/index.html#_included_propertysource_loaders

So you can just provide a dedicated application-mycustomtest.yml as part of your test resources, in which you override all default settings.

Then you can specify as part of the test, which environments shall be active:

@MicronautTest(environments={"mycustomtest"})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fladdimir