6

サービスレイヤーとビューレイヤーのコンテキスト構成の間に明確な抽象化があり、以下に示すようにそれらをロードしています。

Root application context:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

Web application context:

<servlet>
    <servlet-name>lovemytasks</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/mmapp-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

現在、アプリケーションをテストするために SPRING MVC TEST FRAMEWORK を導入しようとしています。

このためには、実際の Web アプリケーションが動作するのと同じ環境をセットアップする必要があります。

どうやってやるの ?

テストで以下の構成を試して、両方のコンテキストをロードしました。

@ContextConfiguration(locations = { "classpath*:META-INF/spring/applicationContext*.xml",
    "file:src/main/webapp/WEB-INF/spring/mmapp-servlet.xml" })

しかし、そのエラーは言っています

Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Duplicate <global-method-security> detected. 

ルート アプリケーション コンテキストと Web アプリケーション コンテキストの両方でグローバル セキュリティを定義しました。

注: Web アプリケーションを実行しても、上記の問題は発生しません。Spring MVc テストを実行したときにのみ発生します

グローバル セキュリティと 1 つの場所を削除してから、テストの実行時に変換サービスでエラーが発生しました。これは、実際の Spring アプリケーションのようにコンテキストをロードしていないことを警告しました。

ここで、Spring Web アプリケーション環境と同じように使用または動作するように、Spring MVC テスト環境をセットアップしたいと思います。どうすればそれを達成できるか教えてください。

4

2 に答える 2

8

@ContextHierarchyアノテーションを使用します。そのjavadocはそれをよく説明しています。あなたの場合、あなたは使用します

@WebAppConfiguration
@ContextHierarchy({
    @ContextConfiguration(locations = { "classpath*:/META-INF/spring/applicationContext-*.xml" }),
    @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/mmapp-servlet.xml" })
})
于 2013-04-16T02:26:02.127 に答える
1

appContext を meta-inf に入れないでください。

「通常の」方法は、web-inf に spring-servlet.xml を 1 つ持つことです。

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring-servlet.xml</param-value>
    </context-param>

そして、xml ファイル内のさまざまなファイルをインポートします。

<import resource="classpath:beans.xml"/>

テスト用に別の appContent を作成します。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ="classpath:applicationContext-test.xml")
@Transactional
public class MyTest {

Bean はどこかで 2 回ロードされている必要があります。Bean を 2 回インポートして、それらを xml で定義し、注釈も付けていますか?

于 2013-04-11T08:51:03.927 に答える