有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java应用程序无法加载应用程序。yml文件

我正在编写junit测试用例,奇怪的事情正在发生 我有两个数据库

  • 1) 活动(应用程序级别) 2) 测试(测试级别)

我的应用程序结构是

    App
    --> src/main/java
    --> src/main/resource
        application.yml

    --> src/test/java
        src/test/resource
        application.yml

当我运行应用程序时,它加载src/main/resource应用程序。yml文件并存储到内存中,并指向运行正常的活动数据库。 当我再次运行Junit测试用例时,src/main/resource应用程序。yml而不是加载src/测试/资源应用程序。yml

当我强制运行测试用例时,应用程序指向测试数据库 当我停止服务器并再次运行应用程序时,它再次指向测试数据库,而不是活动数据库

应用程序。用于(src/main/resources)或(src/test/resources)的yml

spring:
   profiles.active: local
   aop.proxy-target-class: true

---
spring:
  profiles: local

campaignDB:
  driverClassName: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/campaign
  username: root
  password: Admin@123

juintDB:
  driverClassName: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/test
  username: root
  password: Admin@123

测试级别配置

@RunWith(SpringRunner.class)
public class TestDbConfig {
    @Autowired
    private Environment env;

    @Bean
    @Profile("local")
    public DataSource testDbdatasource() {
        org.apache.tomcat.jdbc.pool.DataSource datasource = new org.apache.tomcat.jdbc.pool.DataSource();
        datasource.setDriverClassName(env.getRequiredProperty("juintDB.driverClassName"));
        datasource.setUrl(env.getRequiredProperty("juintDB.url"));
        datasource.setUsername(env.getRequiredProperty("juintDB.username"));
        datasource.setPassword(env.getRequiredProperty("juintDB.password"));
        return datasource;
    }
}

应用程序级配置

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
    emf.setDataSource(datasource());
    emf.setJpaVendorAdapter(hibernateJpa);
    emf.setPackagesToScan("com.brighttalk.campaign.model");

    Map<String, String> jpaSchema = new HashMap<String, String>();
    jpaSchema.put("hibernate.default_schema",
            env.getRequiredProperty("hibernate.default_schema"));

    jpaSchema.put("hibernate.dialect",
            env.getRequiredProperty("hibernate.dialect"));
    jpaSchema.put("hibernate.format_sql",
            env.getRequiredProperty("hibernate.format_sql"));
    jpaSchema.put("hibernate.hbm2ddl.auto",
            env.getRequiredProperty("hibernate.hbm2ddl.auto"));
    jpaSchema.put("hibernate.show_sql",
            env.getRequiredProperty("hibernate.show_sql"));

    emf.setJpaPropertyMap(Collections.singletonMap(
            "javax.persistence.validation.mode", "none"));
    emf.setJpaPropertyMap(jpaSchema);
    return emf;
}

我期望当我测试用例时,它应该指向测试数据库。 当我运行应用程序时,它应该指向活动数据库


共 (1) 个答案

  1. # 1 楼答案

    应用程序属性应该位于主资源目录下,因此您所经历的行为是正常的

    要实现您正在尝试的操作,您需要通过以下方式创建特定于配置文件的应用程序属性:

    1. 为每个概要文件提供一个单独的文件(例如application-test.yml用于名为test的概要文件),并将其添加到同一目录中
    2. 或者如上所述,在同一个application.yml文件中添加单独的特定于概要文件的属性

    然后,您需要使用活动概要文件test运行集成测试,然后spring boot将获取特定于此概要文件的属性

    @RunWith(SpringRunner.class)
    @SpringBootTest()
    @ActiveProfiles("test")
    public class MyIntegrationTest {
        ....
    }
    

    如果未指定和活动配置文件,则活动配置文件将为default,这意味着spring boot将加载默认属性