【问题标题】:Why isn't the configuration from my application-test.properties being used?为什么我的 application-test.properties 中的配置没有被使用?
【发布时间】: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 和其他网站上阅读了很多不同的内容,但没有一个有效,我觉得这是一个相当简单的问题。

【问题讨论】:

    标签: spring jpa junit h2


    【解决方案1】:

    @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 {
    
      // ...
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-22
      • 2018-09-17
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多