我正在嘗試在 micronaut (3.2.7) 應用程式中撰寫基本的控制器測驗。當我運行它時,它無法啟動,因為它也想創建與資料庫相關的 bean。micronaut-hibernate-jpa、flyway等都在pom.xml中。
我可以以某種方式配置背景關系,這樣它就不會拾取 hikaripool、flyway 和 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"
代碼:
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、metrics 等。所以我想測驗也需要以某種方式忽略默認設定。
uj5u.com熱心網友回復:
您可以application.yml使用(測驗)環境特定的屬性檔案覆寫所有(默認):https ://docs.micronaut.io/latest/guide/index.html#_included_propertysource_loaders
因此,您可以只提供一個專用application-mycustomtest.yml的作為測驗資源的一部分,您可以在其中覆寫所有默認設定。
然后你可以指定作為測驗的一部分,哪些環境應該是活動的:
@MicronautTest(environments={"mycustomtest"})
uj5u.com熱心網友回復:
在 gitter 和 currenlty 上詢問 micronaut 團隊,唯一的選擇是沒有默認配置,并且有多個組態檔用于控制器、repo 和 e2e 測驗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/422901.html
標籤:
下一篇:如何為reqwest撰寫測驗?
