1

春ベースのアプリケーション atm を単体テストしています。最初の問題は、サーバーで一度アプリを起動していない場合、単体テストがすべて失敗することです。最初にサーバー上でアプリを起動 (および停止) すると、単体テストが機能します。

サーバーを起動しないと、次のエラーが発生します。

... java.io.FileNotFoundException: class path resource [META-INF/spring/applicationContext-test.xml] cannot be opened because it does not exist

私のユニットテストは次のように定義されています:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/META-INF/spring/applicationContext-test.xml" })
@TransactionConfiguration
@Transactional
public class InventoryControllerTest extends AbstractTransactionalJUnit4SpringContextTests {

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;

    private AnnotationMethodHandlerAdapter handlerAdapter;

    @Before
    public void setUp() throws Exception {
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        handlerAdapter = applicationContext
            .getBean(AnnotationMethodHandlerAdapter.class);
}
    //... tests
}

私が言ったように、アプリを一度起動したことがあれば、すべて正常に動作します。

そのため、構成の場所を locations = { "classpath/META-INF/spring/applicationContext-test.xml" }) に変更しましたが、努力なしで、上記と同じ例外です。

さらに取得する唯一の方法は、この場所です: locations = { "classpath*:applicationContext-test.xml" }) 次に、この例外が発生します: [javax.sql.DataSource] 型の一致する Bean が依存関係で見つかりません: 少なくとも期待されますこの依存関係のオートワイヤー候補として適格な 1 つの Bean。依存関係の注釈: {}

しかし、これは紛らわしいです。なぜなら、テスト コンテキスト ファイルに確実にデータソースがあるからです。

<bean class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" id="dataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:mem:testdb;sql.syntax_ora=true" />
    <property name="username" value="some" />
    <property name="password" value="some" />
</bean>

EIDT 2

問題が RunWith(...) であることを認識した後、Spring クラスを同時に拡張し、ロケーション パスからすべてのワイルドカードを削除します。私はこの例外を受け取ります:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [META-INF/spring/applicationContext-test.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [META-INF/spring/applicationContext-test.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No persistence unit with name 'persistenceUnitTest' found
... 24 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [META-INF/spring/applicationContext-test.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No persistence unit with name 'persistenceUnitTest' found
... 40 more
Caused by: java.lang.IllegalArgumentException: No persistence unit with name 'persistenceUnitTest' found)
... 47 more

私は本当に助けていただければ幸いです!

前もって感謝します

4

1 に答える 1

2

:と の間classpathにパスが必要であり、パスが で始まってはなりません/。したがって、正しい構文は次のようになります。

@ContextConfiguration(locations = { "classpath:META-INF/spring/applicationContext-test.xml" })

または少し短い形式

@ContextConfiguration("classpath:META-INF/spring/applicationContext-test.xml")

自分で見つけた別の問題は、@ContextConfigurationORを使用する必要があることですAbstractTransactionalJUnit4SpringContextTests。以下は、Java Doc からのアコーディング ノートです。AbstractTransactonalJUnit4SpringContextTests

> 注: このクラスは、拡張の利便性のみを提供します。テスト クラスを Spring 固有のクラス階層に関連付けたくない場合は、{@link SpringJUnit4ClassRunner}、{@link ContextConfiguration @ContextConfiguration}、{@link TestExecutionListeners @TestExecutionListeners を使用して、独自のカスタム テスト クラスを構成できます。 }、{@link Transactional @Transactional} など。


最初の問題: Eclipse はリソースをsrc\test\resourcesターゲット ディレクトリにコピーしません。したがって、これを行うツールまたは何かが必要です。1 つの方法を見つけました: アプリケーションを開始します。2 つ目maven testは日食から実行されます。

于 2011-10-27T10:34:41.603 に答える