2012-03-28 102 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

相关问题