我有一個在 mySQL 上運行的小型資料庫應用程式。
我想使用 H2 進行測驗。
我向 build.gradle 添加了必要的依賴項:
runtimeOnly 'com.h2database: h2'
plugins {
id 'org.springframework.boot' version '2.7.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'idea'
}
group = 'com.myprojects'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter:2.7.0'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'javax.validation:validation-api:2.0.1.Final'
implementation 'org.springframework.boot:spring-boot-starter-web:2.7.0'
implementation 'org.jetbrains:annotations:23.0.0'
implementation 'org.springframework.boot:spring-boot-starter-test:2.7.0'
implementation 'junit:junit:4.13.2'
testImplementation 'junit:junit:4.13.2'
runtimeOnly 'mysql:mysql-connector-java'
runtimeOnly 'com.h2database:h2'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
jar {
enabled = false
}
test {
useJUnitPlatform()
}
但是,我注意到在進行測驗之后,我的 mySQL 資料庫包含測驗期間生成的欄位,好像 spring 沒有使用 H2。
有什么問題?
uj5u.com熱心網友回復:
如果您使用
注意:此檔案中不存在的每個必要條目都將退回到您src目錄中的屬性檔案。這可能會導致您的應用程式設定出現一些不一致的情況。
--- 以下部分也適用于非 Gradle 構建:---
如果要在特定測驗中使用特定屬性,可以使用@TestPropertySource注解。
有了這個,您現在可以為大多數測驗和特定測驗的特定屬性定義默認屬性,這意味著@TestPropertySource注釋的順序高于application.properties測驗目錄中的順序并覆寫它們。
uj5u.com熱心網友回復:
您需要配置 application.properties / yaml,如下所示:
spring.datasource.url=jdbc:h2:mem:dbtest
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=user
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
你可以在這里看到更多:https ://www.baeldung.com/spring-boot-h2-database
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/497533.html
下一篇:已發布的物件未更新
