使用 Maven 测试时,Spring Boot 应用程序不读取 application.properties 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22078254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot Application not reading application.properties file when using Maven test
提问by John F.
UPDATE:
更新:
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.
我现在意识到了一些事情。我的 application.properties 文件正在正确加载,因为我通过 /env 路径(感谢 Dave)验证了我的数据库属性正在加载。问题似乎是当我使用 Spring Boot maven 插件运行它时,它无法初始化我的数据源。
mvn spring-boot:run
mvn spring-boot:run
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.
这会导致我的应用程序因错误而崩溃,因为其他 bean 无法初始化。奇怪的是它在 Eclipse 中运行良好。
I have a class called DataService that extends JdbcTemplate. In my DataService constructor, I inject the Datasource.
我有一个名为 DataService 的类,它扩展了 JdbcTemplate。在我的 DataService 构造函数中,我注入了数据源。
@Component
public class DataService extends JdbcTemplate {
@Autowired
public DataService(DataSource dataSource){
super(dataSource);
}
...more custom methods
}
I use this DataService class in other beans to perform DB operations. My DataSource is defined in my application.properties
file
我在其他 bean 中使用这个 DataService 类来执行数据库操作。我的数据源在我的application.properties
文件中定义
spring.datasource.url: jdbc:h2:tcp://localhost/~/testdb2
spring.datasource.driverClassName: org.h2.Driver
This is my Application.java class
这是我的 Application.java 类
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableWebMvcSecurity
@EnableAsync
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
I first realized this when I was attempting to run jUnit tests from Maven using
当我尝试使用 Maven 从 Maven 运行 jUnit 测试时,我第一次意识到这一点
mavent test
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.
我认为这与它如何执行 junit 测试用例有关,但是当我简单地尝试使用 maven 运行应用程序时也会发生这种情况。
My JUnit4 test class is defined as follows:
我的 JUnit4 测试类定义如下:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes={Application.class})
@WebAppConfiguration
public class QuestionRepositoryIntegrationTests {
...methods
}
I used the example from the Spring Boot how-to docs (http://projects.spring.io/spring-boot/docs/docs/howto.html)
我使用了 Spring Boot how-to 文档中的示例(http://projects.spring.io/spring-boot/docs/docs/howto.html)
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.
当我从 Eclipse 运行这个 JUnit 类时,它工作得很好。当它从 maven 执行时,它开始像我上面描述的那样行动。
回答by Antonino Barila
Try to define the <resources>
tag in the build section in your pom, setting path for resource directory where is application.properties
:
尝试<resources>
在你的 pom 的构建部分定义标签,设置资源目录的路径是application.properties
:
<build>
<resources>
<resource>
<directory>resources</directory>
<targetPath>${project.build.outputDirectory}</targetPath>
<includes>
<include>application.properties</include>
</includes>
</resource>
</resources>
</build>
回答by nijogeorgep
You can configure your main datasource as the following, I'm using mysql here. But you can use your own datasource. you can configure the following in your application.propertiesinside src/main/resources
您可以将主数据源配置如下,我在这里使用 mysql。但是您可以使用自己的数据源。您可以在src/main/resources中的application.properties 中配置以下内容
spring.datasource.url = jdbc:mysql://localhost:3306/dsm
spring.datasource.username = root
spring.datasource.password = admin123
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
to run test inside your application you can either use the same datasource or create application-test.propertiesinside src/test/resourcesand its possible to configure test data source over there.
要在您的应用程序中运行测试,您可以使用相同的数据源或在src/test/resources 中创建application-test.properties并且可以在那里配置测试数据源。
回答by Levent Divilioglu
Just add the following statement;
只需添加以下语句;
@TestPropertySource("classpath:application.properties")
To your test class. I am assuming you have your application.properties file under src/test/resources
到你的测试班。我假设你在src/test/resources下有你的 application.properties 文件
Here is my working example;
这是我的工作示例;
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource("classpath:application.properties")
public class TestTwitterFeedRoute extends CamelTestSupport {
//...
}
回答by Vishnu Dahatonde
If you're using eclipse, its always a good idea to check the projects build resources. (Rclick project->properties->build path) I was not getting my application.properties file picked up and turned out I simply missed adding it to build resources.
如果您使用 eclipse,检查项目构建资源总是一个好主意。(Rclick project->properties->build path) 我没有得到我的 application.properties 文件,结果我只是错过了将它添加到构建资源中。
回答by obesechicken13
Make sure your @ConfigurationProperties annotation is set to the same prefix as whatever you're using in your configuration file (application.config)
确保您的 @ConfigurationProperties 注释设置为与您在配置文件 (application.config) 中使用的任何内容相同的前缀
回答by luboskrnac
This works for me:
这对我有用:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestApplication.class,
initializers = ConfigFileApplicationContextInitializer.class)
public class SomeTestClass {
...
}