Spring Boot应用程序在使用Maven测试时没有读取application.properties文件

 a105451223 发布于 2023-01-20 20:32

更新:

我现在意识到了几件事.我的application.properties文件正在正确加载,因为我通过/ env路径验证(感谢Dave)我的数据库属性正在加载.问题似乎是当我使用Spring Boot maven插件运行它时,它无法初始化我的dataSource.

mvn spring-boot:run

这会导致我的应用程序出现错误,因为其他bean无法初始化.奇怪的是它从Eclipse运行良好.

我有一个名为DataService的类,它扩展了JdbcTemplate.在我的DataService构造函数中,我注入了Datasource.

@Component
public class DataService extends JdbcTemplate  {

    @Autowired
    public DataService(DataSource dataSource){
        super(dataSource);
    }
    ...more custom methods
}

我在其他bean中使用此DataService类来执行数据库操作.我的DataSource在我的application.properties文件中定义

spring.datasource.url: jdbc:h2:tcp://localhost/~/testdb2
spring.datasource.driverClassName: org.h2.Driver

这是我的Application.java类

@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableWebMvcSecurity
@EnableAsync
@EnableScheduling
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }    
}

当我尝试使用Maven运行jUnit测试时,我首先意识到了这一点

mavent test

我认为它只是与它如何执行junit测试用例有关,但是当我简单地尝试使用maven运行应用程序时它也会发生.

我的JUnit4测试类定义如下:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes={Application.class})
@WebAppConfiguration
public class QuestionRepositoryIntegrationTests {
     ...methods
}

我使用了Spring Boot how-to docs中的示例(http://projects.spring.io/spring-boot/docs/docs/howto.html)

当我从Eclipse运行这个JUnit类时,它运行得很好.当它从maven执行时,它开始按照我的描述进行操作.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有