2012-03-28 67 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); 
    } 
} 

我也有多個形式的ApplicationContexts。對IDS已經每個文件

<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> 

我想有一個不同的ApplicationContext測試每個單獨的日誌等級改變。

然而,當我運行命令

mvn test 

萬無一失,當配置爲

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

我得到以下

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

一種可能性爲下面一行中的每個測試指定確切的上下文文件名,而不是通配符。

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

我試過這樣做,我得到了同樣的結果。 – Pram 2012-03-28 07:27:55

相關問題