2017-09-27 25 views
0

我有一个特殊的性能测试资源目录中的文件为什么spring boot首先在测试资源中不使用application.properties?

└── test 
    ├── java 
    │ └── com 
    │  └── inter3i 
    │    ├── dao 
    │    │ └── FooMapperTest.java 
    └── resources 
     └── application.properties 
在此

的application.properties文件我指定的MySQL URL。

spring.datasource.url=jdbc:mysql://139.224.xxx.xxx/foo?useSSL=false 

然后我执行测试

mvn test -Dtest=com.foo.reportapi.dao.FooMapperTest 

但失败了,因为

org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. 
    at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:289) ~[spring-jdbc-4.3.10.RELEASE.jar:4.3.10.RELEASE] 

但实际上MySQL的URK是确定的,为什么它有这样的错误?从Wireshark的我知道它实际连接到另一个URL

spring.datasource.url=jdbc:mysql://192.168.0.25/foo 

application-default.properties

src 
├── main 
│ └── resources 
│  ├── application-default.properties 

配置那么,为什么会这样有悖常理?我认为测试类首先应该在测试资源中使用application.properties

此外,我必须使用wireshark来查找它连接到哪个URL,我怎么能得到Spring Boot明确输出MySQL URL信息?

+1

每https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html'应用程序 - {profile}'比'application'具有更高的优先级。 – jonrsharpe

回答

0

正如jonrsharpe已经提到的,特定的轮廓具有在application.properties文件的优先级 - 在这里你找到PropertySource秩序的文件:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

您可以修复它在serveral的方式:

  1. 将main/resources/application-default.properties重命名为main/resources/application.properties
  2. 将test/resources/application.propertie重命名为s to test/resources/application-default.properties
  3. 将test/resources/application.properties重命名为test/resources/application-default-integrationtest.properties并在您的测试类上启用具有以下注释的配置文件:@ActiveProfiles( {“integrationtest”})

我会推荐#3,因为它不依赖于主要和测试元素的类路径优先级,并明确指出使用哪个文件。

现在到您的问题的日志记录部分。

如果将弹簧日志级别增加到“调试”,您可以看到加载了哪些配置文件。你可以在自己的代码登录一个特定的属性:

@Component 
@Slf4j 
public class LogSpringDatasourceUrlProperty { 
    @Autowired 
    public LogSpringDatasourceUrlProperty(@Value("${spring.datasource.url}") String jdbcUrl){ 
    log.info("application uses '{}' as jdbcUrl", jdbcUrl);  
    } 
} 
相关问题