问题
我正在尝试为我的程序中用于验证表单的简单bean编写单元测试。该bean注释为@Component
,并具有使用@Value("${this.property.value}") private String thisProperty;
初始化的类变量
我想为这个类中的验证方法编写单元测试,但是,如果可能的话,我想在不使用属性文件的情况下这样做。我的理由是,如果我从属性文件中提取的值发生变化,我希望这不会影响我的测试用例。我的测试用例是测试验证值的代码,而不是值本身。
有没有办法在我的测试类中使用Java代码初始化Java类并在该类中填充Spring @Value属性然后使用它来测试?
我确实发现这个似乎很接近的How To,但仍然使用属性文件。我宁愿这一切都是Java代码。
谢谢
#1 热门回答(112 赞)
如果可能,我会尝试在没有Spring Context的情况下编写这些测试。如果在没有弹簧的情况下在测试中创建此类,则可以完全控制其字段。
要设置@value
字段,你可以使用SpringsReflectionTestUtils
-它有一个方法setField
来设置私有字段。
@ seeJavaDoc: ReflectionTestUtils.setField(java.lang.Object, java.lang.String, java.lang.Object)
#2 热门回答(90 赞)
从Spring 4.1开始,你可以通过在Unit Tests类级别使用org.springframework.test.context.TestPropertySource
annotation在代码中设置属性值。即使将属性注入到依赖的bean实例中,也可以使用此方法
例如
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = FooTest.Config.class)
@TestPropertySource(properties = {
"some.bar.value=testValue",
})
public class FooTest {
@Value("${some.bar.value}")
String bar;
@Test
public void testValueSetup() {
assertEquals("testValue", bar);
}
@Configuration
static class Config {
@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
return new PropertySourcesPlaceholderConfigurer();
}
}
}
**注释:**在Spring上下文中有必要使用org.springframework.context.support.PropertySourcesPlaceholderConfigurer
的实例
**编辑24-08-2017:**如果你使用的是SpringBoot 1.4.0及更高版本,则可以使用@SpringBootTest
和@SpringBootConfiguration
注释初始化测试。更多信息here
对于SpringBoot,我们有以下代码
@SpringBootTest
@SpringBootConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = {
"some.bar.value=testValue",
})
public class FooTest {
@Value("${some.bar.value}")
String bar;
@Test
public void testValueSetup() {
assertEquals("testValue", bar);
}
}
#3 热门回答(40 赞)
如果需要,你仍然可以在Spring Context中运行测试,并在Spring配置类中设置所需的属性。如果你使用JUnit,请使用SpringJUnit4ClassRunner并为你的测试定义专用配置类:
被测试的课程:
@Component
public SomeClass {
@Autowired
private SomeDependency someDependency;
@Value("${someProperty}")
private String someProperty;
}
测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SomeClassTestsConfig.class)
public class SomeClassTests {
@Autowired
private SomeClass someClass;
@Autowired
private SomeDependency someDependency;
@Before
public void setup() {
Mockito.reset(someDependency);
@Test
public void someTest() { ... }
}
以及此测试的配置类:
@Configuration
public class SomeClassTestsConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer properties() throws Exception {
final PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
Properties properties = new Properties();
properties.setProperty("someProperty", "testValue");
pspc.setProperties(properties);
return pspc;
}
@Bean
public SomeClass getSomeClass() {
return new SomeClass();
}
@Bean
public SomeDependency getSomeDependency() {
// Mockito used here for mocking dependency
return Mockito.mock(SomeDependency.class);
}
}
话虽如此,我不推荐这种方法,我只是在这里添加它以供参考。在我看来,更好的方法是使用Mockito跑步者。在这种情况下,你根本不在Spring内部运行测试,这更加清晰和简单。