@SpringApplicationContext
私のアプリケーションには、 (異なるモジュールからの異なるSpring xmlを使用して)使用するUnitilsベースの統合テストクラスがたくさんあります。作成されるスプリング コンテキストの数を減らすために、ベース テスト クラスを作成し、このベース クラスからのみスプリング コンテキストを初期化し、他のすべてのテストをこのベース クラスを拡張するようにすることを計画しました。
基本クラス:
com.org.module1.mypack;
@SpringApplicationContext({ "spring-module1.xml", "spring-module2.xml", "spring-module3.xml" }) public 抽象クラス BaseTest{ ... }
変更前の子クラス:
com.org.module1.mypack.performance;
@SpringApplicationContext({ "spring-module4.xml" }) public class ChildTest extends BaseTest{ .. }
変更後の基本クラス:
com.org.module1.mypack;
@SpringApplicationContext({ "spring-module1.xml", "spring-module2.xml", "spring-module3.xml", "spring-module4.xml" }) public 抽象クラス BaseTest{ ... }
変更後の子クラス:
com.org.module1.mypack.performance;
//@SpringApplicationContext({ "spring-module4.xml" }) public class ChildTest extends BaseTest{ .. }
以下のエラー メッセージが表示され、コンテキストが作成されません。
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.org.module5.prepare.MyBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=myBean)}
spring-module4.xml が子クラスにある場合は、コンテキストが正常に作成され、すべてのテストが通常どおり実行されました。
注: spring-module4.xml が src/test/resources にあることを除いて、すべての spring xml ファイルは src/main/resources にあります。