2012-03-28 18 views
2

いくつかのコードをテストして、Windowsイベントログに項目を挿入しています。複数のApplicationContextを使用してSpringコンポーネントをテストする

package applicationmonitoring.notify; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import applicationmonitoring.core.Alert; 


@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations ={"classpath:applicationmonitoring/notify/*.xml"}) 
public class WinEventLogNotifierTest { 

    @Autowired 
    protected WinEventLogNotifier winEventLogNotifier; 

    @Autowired 
    protected Alert alert; 

    @org.junit.Before 
    public void setUp() throws Exception { 

    } 

    @org.junit.After 
    public void tearDown() throws Exception { 

    } 

    @Test 
    public void testNotify() throws Exception { 
     winEventLogNotifier.notify(alert); 
    } 
} 

私はフォームの複数のApplicationContextも持っています。ファイルごとにIDが変更されました

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:util="http://www.springframework.org/schema/util" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/util 
http://www.springframework.org/schema/context"> 

    <!-- Calypso notifier --> 
    <bean id="winInfoEventAlertNotifier" class="applicationmonitoring.notify.WinEventLogNotifier" > 
     <constructor-arg index="0"> 
      <value>Windows Event alert notifier</value> 
     </constructor-arg> 
     <constructor-arg index="1"> 
      <map> 
       <!-- Valid Logging Levels and Precendences are --> 
       <!-- DEBUG < INFO < WARN < ERROR < FATAL --> 
       <entry key="loggingLevel" value="INFO"/> 
       <entry key="patternLayout" value="%-4r [%t] %-5p %c %x - %m%n"/> 
      </map> 
     </constructor-arg> 
     <constructor-arg index="2"> 
      <map> 
       <entry key="60" value="20"/> 
       <entry key="3600" value="50"/> 
       <entry key="14400" value="100"/> 
      </map> 
     </constructor-arg> 
    </bean> 

    <bean id="infoAlert" class="applicationmonitoring.core.Alert"> 

     <constructor-arg index="0" value="INFO"/> 
     <constructor-arg index="1" value="infoAlert message"/> 
     <constructor-arg index="2" value="infoAlert source"/> 
     <constructor-arg index="3" value="infoAlert subject"/> 
     <constructor-arg index="4" ref="defaultDate"/> 
     <constructor-arg index="5" ref="defaultException"/> 
    </bean> 

    <bean id="defaultDate" class="java.util.Date" scope="prototype"/> 
    <bean id="defaultException" class="java.lang.Exception"> 
     <constructor-arg index="0" value="Test Exception"/> 
    </bean> 

</beans> 

個々のlogLevelをテストするために異なるApplicationContextを使用したいと思います。

の確実が

<build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.12</version> 
      </plugin> 
     </plugins> 
    </build> 

として設定されている場合、私は、コマンド

mvn test 

を実行したときしかし、私はテストがそれぞれのために実行してもらうにはどうすればよい以下

Tests in error: 
    testNotify(applicationmonitoring.notify.WinEventLogNotifierTest): Error creating bean with name 'applicationmonitoring.notify.WinEventLogNotifierTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected applicationmonitoring.notify.WinEventLogNotifier applicationmonitoring.notify.WinEventLogNotifierTest.winEventLogNotifier; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [applicationmonitoring.notify.WinEventLogNotifier] is defined: expected single matching bean but found 2: [winInfoEventAlertNotifier, winWarnEventAlertNotifier] 

を取得ApplicationContextは孤立していますか?これは正しいアプローチですか?この記事を参照してhttp://bmocanu.ro/coding/399/unit-testing-with-spring-a-bad-mix/は、テスト環境でSpringを使用するのは、統合テストにのみ使用する必要がありますか?

答えて

0

ワイルドカードではなく、下の行で各テストの正確なコンテキストファイル名を指定する可能性が1つあります。

@ContextConfiguration(locations ={"classpath:applicationmonitoring/notify/*.xml"}) 
+0

私はこれをやってみましたが、私は同じ結果を得ました。 – Pram

関連する問題