我正在嘗試在需要使用嵌入式 h2 資料庫(而不是我用于實際應用程式的 sql 資料庫)的 Spring 應用程式中運行測驗。我面臨的問題是它正在以大寫形式對 h2 資料庫執行所有查詢,因此它失敗了。
org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "MOOD" not found; SQL statement:
insert into mood
現在我發現在應用程式屬性中您可以將 DATABASE_TO_LOWER=TRUE 放在連接字串中,但這對我不起作用。我可以清楚地看到輸出中沒有使用我在 application-test.properties 中定義的 url:
Starting embedded database: url='jdbc:h2:mem:85afe142-af28-4c3a-8226-0a77abdb004d;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
所以相關檔案:
我的應用程式-test.properties:
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY-2;DATABASE_TO_LOWER=TRUE
spring.datasource.username=sa
spring.datasource.password=sa
我的測驗課:
package com.example.demo.persistence.interfaces;
import com.example.demo.persistence.dto.Mood;
import com.example.demo.persistence.dto.MoodType;
import com.example.demo.persistence.dto.Patient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import java.util.Calendar;
import java.util.Collection;
import static org.junit.jupiter.api.Assertions.*;
@DataJpaTest
@ActiveProfiles("test")
@TestPropertySource(locations="classpath:application-test.properties")
@EnableConfigurationProperties
class MoodRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private MoodRepository repository;
@Autowired
private PatientRepository patientRepository;
@Test
void getMoodsByPatient() {
final Patient patient = new Patient("Kees", "spek", "straat", Calendar.getInstance().getTime());
final Patient patient2 = new Patient("Kees2", "spek2", "straat2", Calendar.getInstance().getTime());
final Mood mood1 = new Mood(patient, MoodType.GOOD,"","","","","","","",Calendar.getInstance().getTime());
final Mood mood2 = new Mood(patient, MoodType.GOOD,"","","","","","","",Calendar.getInstance().getTime());
final Mood mood3 = new Mood(patient2, MoodType.GOOD,"","","","","","","",Calendar.getInstance().getTime());
entityManager.persist(patient);
entityManager.persist(patient2);
entityManager.persist(mood1);
entityManager.persist(mood2);
entityManager.persist(mood3);
final int expectedMoodsFound = 2;
final int actualMoodsFound = ((Collection<?>)repository.getMoodsByPatient(patient)).size();
assertEquals(expectedMoodsFound,actualMoodsFound);
}
@Test
void getMoodByMoodId() {
}
@Test
void deleteAllByDateBefore() {
}
}
所以我的問題是,我猜我似乎無法正確加載 application-test.properties ?我究竟做錯了什么?我在 stackoverflow 和其他網站上閱讀了很多不同的東西,但都沒有奏效,我覺得這是一個相當簡單的問題。
uj5u.com熱心網友回復:
的@DataJpaTest注釋使用元注釋@AutoConfigureTestDatabase具有默認配置替換任何手工配置或自動配置的資料源:
public @interface AutoConfigureTestDatabase {
/**
* Determines what type of existing DataSource bean can be replaced.
* @return the type of existing DataSource to replace
*/
@PropertyMapping(skip = SkipPropertyMapping.ON_DEFAULT_VALUE)
Replace replace() default Replace.ANY;
/**
* What the test database can replace.
*/
enum Replace {
/**
* Replace the DataSource bean whether it was auto-configured or manually defined.
*/
ANY,
/**
* Only replace the DataSource if it was auto-configured.
*/
AUTO_CONFIGURED,
/**
* Don't replace the application default DataSource.
*/
NONE
}
}
如果您想自己定義資料庫配置,您可以選擇退出:
// ... existing annotations
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class MoodRepositoryTest {
// ...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/369871.html
