我试图为我的程序中用于验证表单的简单bean编写单元测试。该bean使用@Component进行注释,并且有一个初始化使用的类变量

@Value("${this.property.value}") private String thisProperty;

我想为这个类中的验证方法编写单元测试,但是,如果可能的话,我想这样做而不使用属性文件。我这样做的原因是,如果我从属性文件中提取的值发生了变化,我希望它不影响我的测试用例。我的测试用例是测试验证值的代码,而不是值本身。

是否有一种方法可以在我的测试类中使用Java代码来初始化一个Java类,并在该类中填充Spring @Value属性,然后使用它来测试?

我确实发现这个如何,似乎是接近,但仍然使用一个属性文件。我宁愿全部都是Java代码。


当前回答

这是一个相当老的问题,我不确定当时是否有这个选项,但这就是为什么我总是喜欢通过构造函数而不是通过值来实现DependencyInjection的原因。

我可以想象你的类可能是这样的:

class ExampleClass{

   @Autowired
   private Dog dog;

   @Value("${this.property.value}") 
   private String thisProperty;

   ...other stuff...
}

您可以更改为:

class ExampleClass{

   private Dog dog;
   private String thisProperty;

   //optionally @Autowire
   public ExampleClass(final Dog dog, @Value("${this.property.value}") final String thisProperty){
      this.dog = dog;
      this.thisProperty = thisProperty;
   }

   ...other stuff...
}

有了这个实现,spring将知道要自动注入什么,但是对于单元测试,您可以做任何您需要的事情。例如,自动装配spring的每个依赖项,并通过构造函数手动注入它们来创建“ExampleClass”实例,或者只使用spring与测试属性文件,或者根本不使用spring并自己创建所有对象。

其他回答

如果可能的话,我会尝试在没有Spring Context的情况下编写这些测试。如果您在没有spring的测试中创建这个类,那么您可以完全控制它的字段。

要设置@value字段,您可以使用Springs ReflectionTestUtils -它有一个方法setField来设置私有字段。

@see JavaDoc: ReflectionTestUtils.setField(java.lang)Object, java . lang。管柱,java . lang . Object)

@ExtendWith(SpringExtension.class)    // @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(initializers = ConfigDataApplicationContextInitializer.class)

希望这能有所帮助。关键是ConfigDataApplicationContextInitializer获取所有道具数据

从Spring 4.1开始,你可以在代码中通过在单元测试类级别上使用org.springframework.test.context.TestPropertySource注释来设置属性值。您甚至可以使用这种方法向依赖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注解初始化测试。更多信息请点击这里

对于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);
  }

}

我使用下面的代码,它为我工作:

@InjectMocks
private ClassNotify classNotify;

@BeforeEach
  void init() {
    closeable = MockitoAnnotations.openMocks(this);
    ReflectionTestUtils.setField(classNotify, "EventType", "test-event");

  }

SpringBoot自动为我们做了很多事情,但当我们使用@SpringBootTest注释时,我们认为所有事情都将由SpringBoot自动解决。

有很多文档,但最少的是选择一个引擎(@RunWith(SpringRunner.class)),并指出将用于创建上下文以加载配置的类(resources/applicationl.properties)。

简单地说,你需要引擎和上下文:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyClassTest .class)
public class MyClassTest {

    @Value("${my.property}")
    private String myProperty;

    @Test
    public void checkMyProperty(){
        Assert.assertNotNull(my.property);
    }
}

当然,如果你查看Spring Boot文档,你会发现数千种操作系统的方法。