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

时间:2014-02-27 19:18:02

标签: spring maven junit4 spring-boot

更新:

我现在意识到了一些事情。我的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执行时,它开始按照我的描述进行操作。

7 个答案:

答案 0 :(得分:5)

尝试在pom的build部分中定义<?php namespace your-namespace; use Illuminate\Foundation\Http\FormRequest; class MyFormRequest extends FormRequest { public function rules() { return [ 'field' => 'required|unique:table,column,user_id,' . $this->input('field-or-whatever-you-need-from-request'), ]; } } ?> 标记,设置资源目录的路径<resources>

application.properties

答案 1 :(得分:5)

您可以将主数据源配置如下,我在这里使用mysql。但您可以使用自己的数据源。您可以在 src / main / resources

中的 application.properties 中配置以下内容
function loadContentPage(page){
var url = "{{ url('/') }}/";
    $('#content-wrapper').children().fadeOut(150, function(){
     if(page != 'index') { 
       url += page;
      } //end if you have other params update the url based on your routes
        $('#content-wrapper').load(url, function(data){
            $(this).html(data);
        });
    });
}

在您的应用程序中运行测试  可以使用相同的数据源,也可以在 src / test / resources 中创建 application-test.properties ,并可以在那里配置测试数据源

答案 2 :(得分:0)

确保将@ConfigurationProperties注释设置为与配置文件(application.config)中使用的相同的前缀

答案 3 :(得分:0)

这对我有用:

:before

答案 4 :(得分:0)

只需添加以下语句;

@TestPropertySource("classpath:application.properties")

参加您的考试班。我假设您在 src / test / resources

下有application.properties文件

这是我的工作示例;

@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource("classpath:application.properties")
public class TestTwitterFeedRoute extends CamelTestSupport {
//...
}

答案 5 :(得分:0)

如果您使用的是eclipse,检查项目构建资源总是一个好主意。 (单击“项目”->“属性”->“构建路径”) 我没有拿起application.properties文件,结果只是错过了添加它来构建资源。

答案 6 :(得分:0)

我正在尝试从jar外部加载所有属性文件,在pom中的以下条目非常完美,您还可以根据需要排除或包含文件。

  <build>
        <resources>
            <resource>
                 <directory>config</directory>
                 <includes>FILENAME</includes>
                 <excludes>FILENAME</excludes>
            </resource>
        </resources>
    </build>