代码之家  ›  专栏  ›  技术社区  ›  Wim Deblauwe

在单元测试中为@Import弹簧配置设置@Value属性?

  •  0
  • Wim Deblauwe  · 技术社区  · 10 年前

    如何设置 @Value 属性 @Import 进入我的单元测试?

    例如:

    @Configuration
    public void DatabaseTestsConfiguration
    {
      @Value("${test.generateddl:true}")
      private boolean generateDdl;
    
      @Bean
      public JpaVendorAdapter jpaVendorAdapter()
      {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setShowSql( false );
        hibernateJpaVendorAdapter.setGenerateDdl( generateDdl );
        hibernateJpaVendorAdapter.setDatabase( Database.H2 );
        return hibernateJpaVendorAdapter;
      }
    
       // Some more @Bean's here
    }
    

    在我的单元测试中:

    @Test
    @ContextConfiguration
    public void SomeTest extends AbstractTransactionalTestNGSpringContextTests
    {
    
      @Configuration
      @Import(DatabaseTestsConfiguration.class)
      public static class TestConfiguration
      {
      }
    }
    

    现在如何重写属性 test.generateddl 成为 false 在我的单元测试中?注意,我只想在这个单元测试中使用它,所以在命令行中指定一些东西是不可行的。

    1 回复  |  直到 10 年前
        1
  •  2
  •   sodik    10 年前

    您可能需要检查 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/context/TestPropertySource.html#properties--

    弹簧示例 docu :

    @ContextConfiguration
    @TestPropertySource(properties = { "timezone = GMT", "port: 4242" })