用于测试的 Micronaut 上下文配置
Micronaut context configuration for test
我正在尝试在 micronaut (3.2.7) 应用程序中编写基本控制器测试。当我 运行 它时,它无法启动,因为它也想创建与数据库相关的 bean。 micronaut-hibernate-jpa、flyway等都在pom.xml.
我能否以某种方式配置上下文,使其不获取 hikaripool、flyway 和 jpa realted bean?
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"
代码:
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() {
...
}
}
我试图排除这样的配置,但没有成功:
server = ApplicationContext.builder("test")
.exclude("io.micronaut.configuration.hibernate.jpa","io.micronaut.configuration.jdbc.hikari")
.run(EmbeddedServer.class);
注意:如果我从 application.yml 中删除所有内容,则测试有效。看起来在测试中默认属性已解析,这会打开 jpa、指标等。所以我想测试也需要以某种方式忽略默认设置。
您可以使用特定于(测试)环境的 属性 文件覆盖所有(默认)application.yml
:https://docs.micronaut.io/latest/guide/index.html#_included_propertysource_loaders
因此您可以只提供一个专用 application-mycustomtest.yml
作为测试资源的一部分,您可以在其中覆盖所有默认设置。
然后你可以指定作为测试的一部分,which environments shall be active:
@MicronautTest(environments={"mycustomtest"})
在 gitter 和 currenlty 上询问 micronaut 团队,唯一的选择是没有默认配置,并且有多个用于控制器、repo 和 e2e 测试的配置文件。
我正在尝试在 micronaut (3.2.7) 应用程序中编写基本控制器测试。当我 运行 它时,它无法启动,因为它也想创建与数据库相关的 bean。 micronaut-hibernate-jpa、flyway等都在pom.xml.
我能否以某种方式配置上下文,使其不获取 hikaripool、flyway 和 jpa realted bean?
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"
代码:
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() {
...
}
}
我试图排除这样的配置,但没有成功:
server = ApplicationContext.builder("test")
.exclude("io.micronaut.configuration.hibernate.jpa","io.micronaut.configuration.jdbc.hikari")
.run(EmbeddedServer.class);
注意:如果我从 application.yml 中删除所有内容,则测试有效。看起来在测试中默认属性已解析,这会打开 jpa、指标等。所以我想测试也需要以某种方式忽略默认设置。
您可以使用特定于(测试)环境的 属性 文件覆盖所有(默认)application.yml
:https://docs.micronaut.io/latest/guide/index.html#_included_propertysource_loaders
因此您可以只提供一个专用 application-mycustomtest.yml
作为测试资源的一部分,您可以在其中覆盖所有默认设置。
然后你可以指定作为测试的一部分,which environments shall be active:
@MicronautTest(environments={"mycustomtest"})
在 gitter 和 currenlty 上询问 micronaut 团队,唯一的选择是没有默认配置,并且有多个用于控制器、repo 和 e2e 测试的配置文件。