使用Maven测试时Spring Boot Application无法读取application.properties文件

使用Maven测试时Spring Boot Application无法读取application.properties文件

问题描述:

更新:

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

I realized a couple of things now. My application.properties file is being loaded properly because I verified via the /env path (thanks Dave) that my DB properties are being loaded. The problem appears to be that when I run it using the Spring Boot maven plug-in,it fails to initialize my dataSource.

mvn spring-boot:run

然后这会导致我的应用程序因错误而崩溃,因为其他bean无法初始化.奇怪的是,它在Eclipse上运行良好.

This then causes my application to blow-up with errors because other beans can't get initialized. The odd thing is it runs fine from Eclipse.

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

I have a class called DataService that extends JdbcTemplate. In my DataService constructor, I inject the Datasource.

@Component
public class DataService extends JdbcTemplate  {

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

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

I use this DataService class in other beans to perform DB operations. My DataSource is defined in my application.properties file

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

这是我的Application.java类

This is my Application.java class

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

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

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

I first realized this when I was attempting to run jUnit tests from Maven using

mavent test

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

I thought it just had to do with how it was executing the junit test cases however it is also occurring when I simple try to run the application using maven.

我的JUnit4测试类定义如下:

My JUnit4 test class is defined as follows:

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

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

I used the example from the Spring Boot how-to docs (http://projects.spring.io/spring-boot/docs/docs/howto.html)

当我从Eclipse运行此JUnit类时,它工作正常.当它从maven执行时,它开始发挥作用,如上所述.

When I run this JUnit class from Eclipse, it works just fine. When it executes from maven, it starts to act up as I described above.

尝试在pom的build部分中定义<resources>标记,设置资源目录application.properties的路径:

Try to define the <resources> tag in the build section in your pom, setting path for resource directory where is application.properties:

<build>
        <resources>
            <resource>
                <directory>resources</directory>
                <targetPath>${project.build.outputDirectory}</targetPath>
                <includes>
                    <include>application.properties</include>
                </includes>
            </resource>
        </resources>
</build>