使用多个ApplicationContexts测试Spring组件

时间:2012-03-28 06:47:55

标签: spring unit-testing maven maven-3

我有一些测试代码正在测试一些代码,以便将项目插入到Windows事件日志中。

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。每个文件都更改了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>

我希望有一个不同的ApplicationContext来测试每个单独的logLevel。

然而,当我运行命令

mvn test

当surefire配置为

    <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只用于集成测试吗?

1 个答案:

答案 0 :(得分:0)

可以为下面一行中的每个测试指定确切的上下文文件名而不是通配符。

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