数十のBeanを実装する比較的小さなJavaライブラリがあります(データベースやGUIはありません)。他のJavaプロジェクトが自分のBeanを自分のものに注入するために使用するSpringBean構成ファイルを作成しました。
私は今、Spring Testを使用して、これらのBeanの一部を(単にインスタンス化するのではなく)junitテストクラスに注入しようとしているのは初めてです。
私はこれを、一部はSpring Testを学習するために、一部は他の人に提供するのと同じBean構成ファイルを使用するようにテストに強制するために行っています。
Springのドキュメントには、Springに付属の「TestContext」クラスを使用してアプリケーションコンテキストを作成する必要があると書かれています。これは、テストクラスの@ContextConfigurationアノテーションを介して参照するSpringXMLファイルで行う必要があると思います。
@ContextConfiguration({"/test-applicationContext.xml"})
ただし、ファイルに何を入れるかについてのヒントはありません。
Eclipse内からテストを実行しようとすると、「アプリケーションコンテキストのロードに失敗しました」というエラーが表示されます。もちろんです。
アップデート:
test-applicationContext.xmlは次のとおりです。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<description>Holds application context for testing of the domain module.</description>
<!-- Imports the uuid generator bean definitions -->
<import resource="resources/domain-uuid.xml"/>
</beans>
私のプロジェクトディレクトリは次のようなものです。
domain/
src/
main/
java/
resources/
test/
java/
resources/ (location of test-applicationContext.xml)
楽しみのために、「mvn clean test」を介してmvnコマンドラインからビルドしようとしましたが、次のエラーが発生しました。これは私の本当の問題である可能性があります。
package org.springframework.test.context does not exist
package org.springframework.test.context.junit4 does not exist
cannot find symbol
symbol: class ContextConfiguration
@ContextConfiguration({"/resources/test-applicationContext.xml"})
cannot find symbol
symbol: class SpringJUnit4ClassRunner
@RunWith(SpringJUnit4ClassRunner.class)