2014-01-20 15 views
9

1つ以上の統合テストでSpring構成の単一のBeanまたは値を置き換える可能性はありますか?私の場合はテスト用のJavaベースのSpringコンテキスト設定を上書きする

、私は

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = MyIntegrationTestConfig.class, loader = SpringApplicationContextLoader.class) 
public class MyIntegrationTest { 
    // do the tests 
} 

は、今私は私がで1つのBeanを交換する統合テストの第2のセットを持つようにしたい私の統合テストのために使用される構成

@Configuration 
@EnableAutoConfiguration 
@ComponentScan(basePackages = {"foo.bar"}) 
public class MyIntegrationTestConfig { 
    // everything done by component scan 
} 

を持っています別のもの。

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = MyIntegrationTestConfig.class, loader = SpringApplicationContextLoader.class) 
public class MySpecialIntegrationTest { 
    // influence the context configuration such that a bean different from the primary is loaded 

    // do the tests using the 'overwritten' bean 
} 

これを達成する最も簡単な方法は何ですか?

答えて

10

Springのテストフレームワークは、設定の拡張を理解することができます。

@ContextConfiguration(classes = MySpecialIntegrationTestConfig.class, loader = SpringApplicationContextLoader.class) 
public class MySpecialIntegrationTest extends MyIntegrationTest { 

    @Configuration 
    public static class MySpecialIntegrationTestConfig { 
    @Bean 
    public MyBean theBean() {} 
    } 

} 

をし、必要なJava Configクラスを作成し、@ContextConfigurationにそれを提供します。それはあなただけMyIntegrationTestからMySpecialIntegrationTestを拡張する必要があることを意味します。 Springは基本のものをロードし、拡張したテストケースに特化したもので拡張します。

詳細については、official documentationを参照してください。

関連する問題