【发布时间】:2022-01-06 03:38:11
【问题描述】:
我正在尝试在我的 Spring 应用程序中运行需要使用嵌入式 h2 数据库(而不是我用于实际应用程序的 sql 数据库)的测试。我面临的问题是它在大写的 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 和其他网站上阅读了很多不同的内容,但没有一个有效,我觉得这是一个相当简单的问题。
【问题讨论】: