2017-01-25 12 views
4

私はSpringブート1.4.3.RELEASEを使用しており、テストの実行中に一部のコンポーネントをスキャンから除外したい。フィルターにもかかわらずSpringのComponentScan excludeFiltersアノテーションがSpringブートテストコンテキストで機能しない

@RunWith(SpringRunner.class) 
@SpringBootTest 
@ComponentScan(
     basePackages = {"com.foobar"}, 
     excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {AmazonKinesisRecordChecker.class, MyAmazonCredentials.class})) 
public class ApplicationTests { 

    @Test 
    public void contextLoads() { 
    } 

} 

、私はテストを実行すると、不要なコンポーネントがロードされ、これらのクラスが正しく動作するAWS環境を必要とする春ブーツがクラッシュ:

2017-01-25 16:02:49.234 ERROR 10514 --- [   main] o.s.boot.SpringApplication    : Application startup failed 

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'amazonKinesisRecordChecker' defined in file 

質問:どのようにすることができますフィルターを機能させるには?

+0

@SpringBootTest(classes = <<ロードするクラスを選択>>)。ドキュメント:http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html – Barath

+0

@Barathあなたはクラスを含める方法があると言っていますか?春のブートテストでは除外しませんか? – Javide

+0

いいえWebMvcTestを使用してフィルタを除外することができます。Doc:http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/web/servlet /WebMvcTest.html – Barath

答えて

2

@MockBeanを使用すると、除外するのではなく模擬する必要があります。

@RunWith(SpringRunner.class) 
@SpringBootTest 
public class ApplicationTests { 
    @MockBean 
    AmazonCredentials amazonCredentials; 

    @Test 
    public void contextLoads() { 
    } 
} 

この道の下に示されているように、あなたは、Springコンテキストは、あなたがAmazonCredentials Beanを模擬することをお知らせします。場合によっては、フィルターを除外するのが少し難解です。

希望すると便利です。私たちがこれをやり遂げる別の方法があれば探検したいです。

関連する問題