Spring 在测试中使用ConfigurationProperties和EnableConfigurationProperties的惯用方法是什么?
我正在尝试为spring(-boot)应用程序中使用的某些元素设置单元测试,我在Spring 在测试中使用ConfigurationProperties和EnableConfigurationProperties的惯用方法是什么?,spring,spring-boot,Spring,Spring Boot,我正在尝试为spring(-boot)应用程序中使用的某些元素设置单元测试,我在ConfigurationProperties和enableCigurationproperties的设置方面遇到了困难。我最终实现它的方式似乎与我所看到的示例不一致,因为我看到在我的配置类上同时需要ConfigurationProperties和enableCigurationproperties,这似乎不正确,我希望有人能提供一些指导 以下是一个简化的示例: JavaTestConfiguration.java
ConfigurationProperties
和enableCigurationproperties
的设置方面遇到了困难。我最终实现它的方式似乎与我所看到的示例不一致,因为我看到在我的配置类上同时需要ConfigurationProperties
和enableCigurationproperties
,这似乎不正确,我希望有人能提供一些指导
以下是一个简化的示例:
JavaTestConfiguration.java
package com.kerz;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.validation.constraints.NotNull;
@Configuration
@ConfigurationProperties
@EnableConfigurationProperties
public class JavaTestConfiguration {
public void setFoo(String foo) {
this.foo = foo;
}
@NotNull
String foo;
@Bean
String foo() {
return foo;
}
}
package com.kerz;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {JavaTestConfiguration.class})
@TestPropertySource("classpath:test.properties")
public class JavaTestConfigurationTest {
@Autowired
String foo;
@Test
public void shouldWork() throws Exception {
assertEquals("foo", "bar", foo);
}
}
JavaTestConfigurationTest.java
package com.kerz;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.validation.constraints.NotNull;
@Configuration
@ConfigurationProperties
@EnableConfigurationProperties
public class JavaTestConfiguration {
public void setFoo(String foo) {
this.foo = foo;
}
@NotNull
String foo;
@Bean
String foo() {
return foo;
}
}
package com.kerz;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {JavaTestConfiguration.class})
@TestPropertySource("classpath:test.properties")
public class JavaTestConfigurationTest {
@Autowired
String foo;
@Test
public void shouldWork() throws Exception {
assertEquals("foo", "bar", foo);
}
}
test.properties
foo=bar
如果您正在启动Spring上下文,那么您的测试更像是集成测试。因此,您还应该测试产品spring配置 我建议不要创建测试配置。使用一个生产配置进行测试
您还使用了
@TestPropertySource
注释,当您需要定义特定于测试的属性时,可以使用该注释。如果可以使用PROD配置进行测试,请不要使用它 如果您正在启动Spring上下文,那么您的测试就是更多的集成测试。因此,您还应该测试产品spring配置
我建议不要创建测试配置。使用一个生产配置进行测试
您还使用了
@TestPropertySource
注释,当您需要定义特定于测试的属性时,可以使用该注释。如果可以使用PROD配置进行测试,请不要使用它 使用Spring概要文件和与该概要文件关联的普通@Configuration类如何?在这个答案中,我展示了一种方法:使用Spring概要文件和与该概要文件关联的普通@Configuration类如何?在这个答案中,我展示了一种方法: