运行集成测试时加载不同的弹簧配置

时间:2021-01-27 15:42:50

I have the following setup: normally my webapp will be deployed on stand-alone server and connect to MySQL database. However I want ability to "self-test" the application with Selenium. So during mvn clean install there will be embedded server (Jetty 7), Selenium Server and in-memory database (HSQLDB) that will allow to perform some actions (example user inputs for the webapp). Now I already setup Selenium/embedded server setup using maven plugins:

我有以下设置:通常我的webapp将部署在独立服务器上并连接到MySQL数据库。但是,我希望能够使用Selenium“自我测试”应用程序。因此,在mvn clean install期间,将有嵌入式服务器(Jetty 7),Selenium Server和内存数据库(HSQLDB),它们将允许执行某些操作(webapp的示例用户输入)。现在我已经使用maven插件设置了Selenium /嵌入式服务器设置:

<plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.10</version>
            <configuration>
                <!-- Log to the console. -->
                    <requestLog implementation="org.mortbay.jetty.NCSARequestLog">
                    <!-- This doesn't do anything for Jetty, but is a workaround for a Maven bug
                        that prevents the requestLog from being set. -->
                    <append>true</append>
                </requestLog>
            </configuration>
        </plugin>
        <!-- 
        *******************************************************
        Start selenium-server before the integration test start
        ******************************************************* 
        -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>selenium-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>start-selenium-server</id>
                    <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start-server</goal>
                        </goals>
                        <configuration>
                            <background>true</background>
                            <logOutput>true</logOutput>
                            <multiWindow>true</multiWindow>
                        </configuration>
                </execution>
                <execution>
                    <id>stop-selenium-server</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop-server</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- ********************************************************
        Force to run the testcases in the integration-test phase
        ******************************************************** -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <!-- Skip the normal tests, we'll run them in the integration-test phase -->
                <skip>true</skip>
            </configuration>
            <executions>
                <execution>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <skip>false</skip>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- ***********************************************************
        Deploy the war in the embedded jetty of cargo2-maven-plugin
        *********************************************************** -->
        <plugin>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
            <version>1.4.1-SNAPSHOT</version>
            <executions>
                <execution>
                    <id>start-container</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                </execution>
                <execution>
                    <id>stop-container</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <wait>false</wait>
                <container>
                    <containerId>jetty7x</containerId>
                    <type>embedded</type>
                </container>
            </configuration>
        </plugin> 

And it is working just fine. Unfortunately I'm having some issues trying to make Spring/Maven use different XML configuration file when app is being deployed on embedded server for integration testing purposes.

它工作得很好。不幸的是,当在嵌入式服务器上部署应用程序以进行集成测试时,我遇到了一些问题,试图让Spring / Maven使用不同的XML配置文件。

I tried using:

我试过用:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:servlet-context-test.xml" })
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestWebapp extends SeleneseTestCase{

(servlet-context-text.xml is located in src/test/resources) but when Selenium tests run the webapp is still started with default configuration.

(servlet-context-text.xml位于src / test / resources中)但是当Selenium测试运行时,webapp仍然以默认配置启动。

I'm trying to load different xml file because I basically want to use this:

我正在尝试加载不同的xml文件,因为我基本上想要使用它:

<jdbc:embedded-database id="dataSource">
   <jdbc:script location="classpath:sql/schema.sql"/>
   <jdbc:script location="classpath:sql/fk.sql"/>
   <jdbc:script location="classpath:sql/data.sql"/>
</jdbc:embedded-database>

instead of my normal dataSource declaration.

而不是我正常的dataSource声明。

2 个解决方案

#1


0  

You should have a look at the Spring profiles (http://blog.springsource.org/2011/02/14/spring-3-1-m1-introducing-profile/). With a profile, you can activate/deactivate beans in different ways (programamtically, with environment variable etc.)

您应该查看Spring配置文件(http://blog.springsource.org/2011/02/14/spring-3-1-m1-introducing-profile/)。使用配置文件,您可以以不同方式激活/停用Bean(以编程方式,使用环境变量等)

#2


0  

Maybe it will be helpful to someone out there. I finally resolved this issue using Maven profiles (separate profiles for integration testing and for production build) and filtering (parametrized my servlet-context.xml in regards to importing either dataSource.xml or dataSourceIntegrationTesting.xml). Works like a charm.

也许对那里的人有帮助。我最后使用Maven配置文件(用于集成测试和生产构建的单独配置文件)和过滤(在导入dataSource.xml或dataSourceIntegrationTesting.xml方面参数化了我的servlet-context.xml)解决了这个问题。奇迹般有效。

#1


0  

You should have a look at the Spring profiles (http://blog.springsource.org/2011/02/14/spring-3-1-m1-introducing-profile/). With a profile, you can activate/deactivate beans in different ways (programamtically, with environment variable etc.)

您应该查看Spring配置文件(http://blog.springsource.org/2011/02/14/spring-3-1-m1-introducing-profile/)。使用配置文件,您可以以不同方式激活/停用Bean(以编程方式,使用环境变量等)

#2


0  

Maybe it will be helpful to someone out there. I finally resolved this issue using Maven profiles (separate profiles for integration testing and for production build) and filtering (parametrized my servlet-context.xml in regards to importing either dataSource.xml or dataSourceIntegrationTesting.xml). Works like a charm.

也许对那里的人有帮助。我最后使用Maven配置文件(用于集成测试和生产构建的单独配置文件)和过滤(在导入dataSource.xml或dataSourceIntegrationTesting.xml方面参数化了我的servlet-context.xml)解决了这个问题。奇迹般有效。