![](/img/trans.png)
[英]Maven - How to tell Maven to run integration tests with application-integration.properties under src/test/resources?
[英]How to: Run maven integration tests against a test environment (database)
我正在使用maven和maven-failsafe-plugin在集成测试生命周期阶段启动jetty。 然后,我对运行的webapp执行了许多(* IT.java)junit测试。 这是按预期工作的。
但是,我想连接到测试数据库进行集成测试。 我正在存储它的URL
${basedir}/src/test/resources/jdbc.properties
当jetty插件运行(jetty:run)时,它会使用
${basedir}/src/main/resources/jdbc.propertes
代替。 property to use 我尝试通过属性重新配置jetty插件来使用
${project.build.testOutputDirectory}
但是test-classes目录缺少我实际编译的项目类,以及存储的资源
${basedir}/src/main/resources
注意:surefire将测试资源添加到类路径中,然后是主资源,这样两者中的任何内容都将使用测试版本,因为它首先在类路径中找到。
有关如何正确设置此设置的任何想法?
谢谢!
编辑:
好吧,似乎jetty-plugin上有配置属性来处理这个问题:
不幸的是,它们不起作用。
这是我的pom.xml的相关部分:
<testResources> <testResource> <filtering>true</filtering> <directory>src/test/resources</directory> </testResource> </testResources> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.26</version> <configuration> <contextPath>/</contextPath> <stopPort>8005</stopPort> <stopKey>STOP</stopKey> </configuration> <executions> <execution> <id>start-jetty</id> <phase>pre-integration-test</phase> <goals> <goal>run</goal> </goals> <configuration> <daemon>true</daemon> <useTestClasspath>true</useTestClasspath> <testClassesDirectory>${project.build.testOutputDirectory}</testClassesDirectory> </configuration> </execution> <execution> <id>stop-jetty</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-failsafe-plugin</artifactId> <version>2.6</version> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> <configuration> <useFile>false</useFile> </configuration> </plugin>
我有同样的问题,并通过使用自定义web.xml(jettyweb.xml)解决它看到maven configuarion
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
<overrideWebXml>./src/main/webapp/WEB-INF/jettyweb.xml</overrideWebXml>
<scanintervalseconds>3</scanintervalseconds>
</configuration>
<dependencies>
</dependencies>
</plugin>
</plugins>
</build>
在我的例子中,我使用此配置来使用其他一些弹簧配置来管理事务。 但是这种策略也可以用于使用其他属性文件。
我原来的web.xml有这个spring配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-hibernate.xml,
/WEB-INF/spring-services.xml
</param-value>
</context-param>
我的jettyweb.xml有这个弹簧配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-hibernate-jetty.xml,
/WEB-INF/spring-services.xml
</param-value>
</context-param>
这应该会让你走上正确的道路
我用了
<configuration>
<jettyEnvXml>src/test/webapp/WEB-INF/jetty-env.xml</jettyEnvXml>
.
.
内容
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<New id="MyDb" class="org.mortbay.jetty.plus.naming.Resource">
<Arg>jdbc/myDS</Arg>
<Arg>
<New class="org.apache.commons.dbcp.BasicDataSource">
<Set name="driverClassName">org.h2.Driver</Set>
<Set name="url">jdbc:h2:mem:testdb;INIT=CREATE SCHEMA IF NOT EXISTS TESTDB\;SET SCHEMA TESTDB</Set>
<Set name="username">sa</Set>
<Set name="password"></Set>
</New>
</Arg>
</New>
</Configure>
我还需要在我的web.xml中
<resource-ref>
<res-ref-name>jdbc/myDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
我用spring config
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/myDS" />
</bean>
我尝试了两位作者以及@thehpi的建议。 两者都是可信的,但在我尝试它们时遇到了一些问题。 我的设置是以下maven项目,使用与maven-failsafe-plugin
集成测试,使用JPA和persistence.xml文件,用于告诉容器如何连接到数据库。
简而言之,下面详细介绍了如何使用JVM属性来告诉您的真实代码只使用不同的persistence-unit
。 其他解决方案让我在使用带有Jetty 7的Hibernate 4.1时遇到了麻烦(找到数据源的问题)。
唯一的缺点是在项目的发布版本中最终会出现无用的配置。 辅助持久性单元永远不会在maven集成测试之外使用。 我确定有一种方法可以解决它,但对我来说,我对这种方法很满意。 我甚至将hsqldb.jar从构建中拉出来,并使其成为仅对插件执行的依赖。
来自POM的相关信息
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8.1</version>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.2.0.v20101020</version>
<dependencies>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.10</version>
</dependency>
</dependencies>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopPort>8005</stopPort>
<stopKey>STOP</stopKey>
<contextPath>/</contextPath>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
<systemProperties>
<systemProperty>
<name>RUNNING_TESTS</name>
<value>true</value>
</systemProperty>
</systemProperties>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
请注意名为“RUNNING_TESTS”的System属性。
HibernateUtil.java
public class HibernateUtil {
private static final EntityManagerFactory emfInstance;
static {
String istest = System.getProperty("RUNNING_TESTS");
System.out.println("RUNNING_TESTS: " + istest);
if (istest != null && istest.equalsIgnoreCase("true")) {
emfInstance = Persistence.createEntityManagerFactory("integration-tests");
}
else {
emfInstance = Persistence.createEntityManagerFactory("productionname");
}
}
public static EntityManagerFactory getInstance() {
return emfInstance;
}
private HibernateUtil() {
}
}
persistence.xml (在/ src / main / resources / META-INF中)
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<!-- persistence.xml -->
<persistence-unit name="productionname">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:comp/env/jdbc/selectivemailpush</non-jta-data-source>
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
</properties>
</persistence-unit>
<persistence-unit name="integration-tests">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>...</class>
<class>...</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:integration-tests" />
<property name="hibernate.showSql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
我正在努力解决同样的问题,但我认为useTestClasspath似乎不起作用的原因实际上在于Spring。
您可能具有如下配置:
<context:property-placeholder location="classpath*:**/*.properties"/>
如果删除第一个*我认为它有效,因为使用此配置它从不同位置加载相同的属性文件(主要和测试,如果两者都存在)。 删除*只会加载测试版。 所以改成它
<context:property-placeholder location="classpath:**/*.properties"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.