使用多个ApplicationContexts测试Spring组件

使用多个ApplicationContexts测试Spring组件,spring,unit-testing,maven,maven-3,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.ContextConfigura

我有一个测试代码,它正在测试将项目插入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>
当surefire配置为

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

如何让每个ApplicationContext单独运行测试?这是正确的方法吗?参考本文,在测试情况下使用Spring是否仅用于集成测试?

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

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

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

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