为什么我的 application-test.properties 的配置没有被使用?

Why isn't the configuration from my application-test.properties being used?

我正在尝试在我的 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

我的测试class:

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() {
    }
}

所以我的问题是,我似乎无法正确加载应用程序-test.properties 我猜是吧?我究竟做错了什么?我在 Whosebug 和其他网站上阅读了很多不同的内容,其中 none 有效,我觉得这是一个相当简单的问题。

@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 {

  // ...

}