Exi*_*xia 49 spring-mvc spring-boot
我正在使用一个运行我的src/main/resources/config/application.yml的spring启动应用程序.
当我运行我的测试用例时:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
public class MyIntTest{
}
Run Code Online (Sandbox Code Playgroud)
测试代码仍然运行我的application.yml文件来加载属性.我想知道在运行测试用例时是否可以运行另一个*.yml文件.
g00*_*00b 59
一种选择是使用配置文件.创建一个名为application-test.yml的文件,将这些测试所需的所有属性移动到该文件,然后将@ActiveProfiles注释添加到测试类中:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
@ActiveProfiles("test") // Like this
public class MyIntTest{
}
Run Code Online (Sandbox Code Playgroud)
请注意,它还会加载application-test.yml,因此application.yml中的所有属性仍然会被应用.如果您不想这样做,也可以使用配置文件,或者在application-test.yml中覆盖它们.
The*_*ect 19
您可以在src/test/resources/config/application.yml文件中设置测试属性.Spring Boot测试用例将从测试目录中的application.yml文件中获取属性.
Ama*_*har 14
您可以使用@TestPropertySource加载不同的属性/ yaml文件
@TestPropertySource(locations="classpath:test.properties")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class MyIntTest{
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您只想覆盖您可以使用的特定属性/ yaml
@TestPropertySource(
properties = {
"spring.jpa.hibernate.ddl-auto=validate",
"liquibase.enabled=false"
}
)
Run Code Online (Sandbox Code Playgroud)
Spring-boot框架允许我们提供YAML文件作为.properties 文件的替代,它很方便。属性文件中的键可以在资源文件夹中的application.yml文件中以 YAML 格式提供,spring-boot 会自动取请记住,yaml 格式必须保持空格正确才能正确读取值。
您可以使用@Value("${property}")来从 YAML 文件中注入值。还可以给出Spring.active.profiles来区分不同环境的不同 YAML,方便部署。
出于测试目的,测试 YAML 文件可以命名为application-test.yml并放置在测试目录的资源文件夹中。
如果您application-test.yml在 .yml中指定并提供 spring 测试配置文件,那么您可以使用@ActiveProfiles('test')注释来指示 spring 从您指定的application-test.yml获取配置。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApplicationTest.class)
@ActiveProfiles("test")
public class MyTest {
...
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 JUnit 5,则不需要其他注释,因为@SpringBootTest已经包含 springrunner 注释。保留一个单独的主 ApplicationTest.class 使我们能够为测试提供单独的配置类,我们可以通过将它们从测试主类中的组件扫描中排除来防止加载默认配置 bean。您还可以提供要在那里加载的配置文件。
@SpringBootApplication(exclude=SecurityAutoConfiguration.class)
public class ApplicationTest {
public static void main(String[] args) {
SpringApplication.run(ApplicationTest.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
这是有关使用 YAML 而不是.properties 文件的Spring 文档的链接:https : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
如果您需要application.yml完全替换生产,则将其测试版本放到相同的路径,但要在测试环境中使用(通常是src/test/resources/)
但是,如果您需要覆盖或添加一些属性,则几乎没有选择。
选项1:将测试application.yml放在src/test/resources/config/目录中,如@TheKojuEffect在其答案中建议的那样。
选项2:使用特定application-test.yml于配置文件的属性:在您的src/test/resources/文件夹中创建“说” ,然后:
@ActiveProfiles在测试类中添加注释:
@SpringBootTest(classes = Application.class)
@ActiveProfiles("test")
public class MyIntTest {
Run Code Online (Sandbox Code Playgroud)或者spring.profiles.active在@SpringBootTest注释中设置属性值:
@SpringBootTest(
properties = ["spring.profiles.active=test"],
classes = Application.class,
)
public class MyIntTest {
Run Code Online (Sandbox Code Playgroud)这不但可以用@SpringBootTest,但用@JsonTest,@JdbcTests,@DataJpaTest等片测试注释为好。
而且,您可以根据需要设置任意多个配置文件(spring.profiles.active=dev,hsqldb)-有关配置文件的更多信息,请参见。
请参阅:使用 YAML 的 Spring @PropertySource
我认为第三个答案有你正在寻找的东西,即有一个单独的 POJO 将你的 yaml 值映射到:
@ConfigurationProperties(path="classpath:/appprops.yml", name="db")
public class DbProperties {
private String url;
private String username;
private String password;
...
}
Run Code Online (Sandbox Code Playgroud)
然后用这个注释你的测试类:
@EnableConfigurationProperties(DbProperties.class)
public class PropertiesUsingService {
@Autowired private DbProperties dbProperties;
}
Run Code Online (Sandbox Code Playgroud)
一个简单的工作配置使用
@TestPropertySource 和属性
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = {"spring.config.location=classpath:another.yml"})
public class TestClass {
@Test
public void someTest() {
}
}
Run Code Online (Sandbox Code Playgroud)
我已经测试了@IlyaSerbis 答案,它对我有用。我正在使用 Spring Boot 2.3。
我已经测试了这些场景并且它有效。
场景 1 -首先,我为 jUnit 目的创建application.yml并将其放在src/test/resources/config(与主 application.yml 相同的路径)下,它完全替换了原始 yaml 文件(而不是覆盖)。application.yml这意味着它不会使用目录(主目录)中提到的任何属性src/main/resources/config。
注意:在Scenario1main和test中application.yml的路径需要完全相同。如果src/main/resources/config/application.yml那么src/test/resources/config/application.yml。如果src/main/resources/application.yml那么src/test/resources/application.yml
场景2 -然后我创建application-test.yml而不是使用application.ymlundersrc/test/resources/config和 using @ActiveProfiles("test"),我可以看到 jUnit 正在从中获取属性值application-test.yml (覆盖公共属性),然后也application.yml从中获取值,这在 中没有提到。application.ymlapplication-test.yml
| 归档时间: |
|
| 查看次数: |
67587 次 |
| 最近记录: |