Spring 启动测试:@TestPropertySource 未覆盖@EnableAutoConfiguration
Spring Boot Test: @TestPropertySource not overriding @EnableAutoConfiguration
我正在使用 Spring 数据 LDAP 从 LDAP 服务器获取用户数据。
我的文件结构如下所示:
main
java
com.test.ldap
Application.java
Person.java
PersonRepository.java
resources
application.yml
schema.ldif
test
java
Tests.java
resources
test.yml
test_schema.ldif
这是我的测试 class:
import com.test.ldap.Person;
import com.test.ldap.PersonRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {PersonRepository.class})
@TestPropertySource(locations = "classpath:test.yml")
@EnableAutoConfiguration
public class Tests {
@Autowired
private PersonRepository personRepository;
@Test
public void testGetPersonByLastName() {
List<Person> names = personRepository.getPersonNamesByLastName("Bachman");
assert(names.size() > 0);
}
}
问题是,Spring Boot 正在加载 application.yml
和 schema.ldif
文件,而不是我的测试 YAML 和 LDIF 文件,尽管事实上我的 @TestPropertySource
注释明确列出 test.yml
。这似乎是由于自动配置,为了方便我更愿意使用它。
我希望 @TestPropertySource
比自动配置具有更高的优先级,但情况似乎并非如此。这是 Spring 中的错误,还是我误解了什么?
作为记录,这是我的 test.yml
文件(它确实指定 test_schema.ldif
):
spring:
ldap:
# Embedded Spring LDAP
embedded:
base-dn: dc=test,dc=com
credential:
username: uid=admin
password: secret
ldif: classpath:test_schema.ldif
port: 12345
validation:
enabled: false
所以我能够通过手动指定使用 LDIF 文件所需的属性来解决这个问题。这是因为,根据 @TestPropertySource 文档,内联属性比 属性 文件具有更高的优先级。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {PersonRepository.class})
@TestPropertySource(properties =
{"spring.ldap.embedded.ldif=test_schema.ldif", "spring.ldap.embedded.base-dn=dc=test,dc=com"})
@EnableAutoConfiguration
public class Tests {
//...
}
但这不是最好的解决方法,但是:如果我需要定义的属性不止两个怎么办?将它们全部列出来是不切实际的。
编辑:
将我的 test.yml
文件重命名为 application.yml
,这样它就可以覆盖生产文件。事实证明,TestPropertySource
注释仅适用于 .properties 文件。
我发现 YML 文件不能使用 @TestPropertySource 注释。
一个干净的方法是使用 @ActiveProfile。假设您的带有测试属性的 YML 文件名为
application-integration-test.yml
那么你应该像这样使用注释
@ActiveProfile("integration-test")
我正在使用 Spring 数据 LDAP 从 LDAP 服务器获取用户数据。
我的文件结构如下所示:
main
java
com.test.ldap
Application.java
Person.java
PersonRepository.java
resources
application.yml
schema.ldif
test
java
Tests.java
resources
test.yml
test_schema.ldif
这是我的测试 class:
import com.test.ldap.Person;
import com.test.ldap.PersonRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {PersonRepository.class})
@TestPropertySource(locations = "classpath:test.yml")
@EnableAutoConfiguration
public class Tests {
@Autowired
private PersonRepository personRepository;
@Test
public void testGetPersonByLastName() {
List<Person> names = personRepository.getPersonNamesByLastName("Bachman");
assert(names.size() > 0);
}
}
问题是,Spring Boot 正在加载 application.yml
和 schema.ldif
文件,而不是我的测试 YAML 和 LDIF 文件,尽管事实上我的 @TestPropertySource
注释明确列出 test.yml
。这似乎是由于自动配置,为了方便我更愿意使用它。
我希望 @TestPropertySource
比自动配置具有更高的优先级,但情况似乎并非如此。这是 Spring 中的错误,还是我误解了什么?
作为记录,这是我的 test.yml
文件(它确实指定 test_schema.ldif
):
spring:
ldap:
# Embedded Spring LDAP
embedded:
base-dn: dc=test,dc=com
credential:
username: uid=admin
password: secret
ldif: classpath:test_schema.ldif
port: 12345
validation:
enabled: false
所以我能够通过手动指定使用 LDIF 文件所需的属性来解决这个问题。这是因为,根据 @TestPropertySource 文档,内联属性比 属性 文件具有更高的优先级。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {PersonRepository.class})
@TestPropertySource(properties =
{"spring.ldap.embedded.ldif=test_schema.ldif", "spring.ldap.embedded.base-dn=dc=test,dc=com"})
@EnableAutoConfiguration
public class Tests {
//...
}
但这不是最好的解决方法,但是:如果我需要定义的属性不止两个怎么办?将它们全部列出来是不切实际的。
编辑:
将我的 test.yml
文件重命名为 application.yml
,这样它就可以覆盖生产文件。事实证明,TestPropertySource
注释仅适用于 .properties 文件。
我发现 YML 文件不能使用 @TestPropertySource 注释。 一个干净的方法是使用 @ActiveProfile。假设您的带有测试属性的 YML 文件名为
application-integration-test.yml
那么你应该像这样使用注释
@ActiveProfile("integration-test")