13

統合テストのライフサイクル段階で、maven と maven-failsafe-plugin を使用して jetty を起動しています。次に、実行中の Web アプリケーションに対して多数の (*IT.java) junit テストを実行します。これは期待どおりに機能しています。

ただし、統合テストのためにテスト データベースに接続したいと考えています。そのURLを保存しています

${basedir}/src/test/resources/jdbc.properties  

jetty プラグインが実行されると (jetty:run)、

${basedir}/src/main/resources/jdbc.propertes 

代わりは。classesDirectoryプロパティを使用して jetty プラグインを再構成してみました

${project.build.testOutputDirectory}

しかし、test-classes ディレクトリには、実際にコンパイルされたプロジェクト クラスと、そこに保存されているリソースがありません。

${basedir}/src/main/resources 

注:surefireはテストリソースをクラスパスに追加し、その後にメインリソースが続きます。両方で見つかったものは、クラスパスで最初に見つかったテストバージョンを使用します。

これを正しく設定する方法についてのアイデアはありますか?

ありがとう!

編集:

これに対処するために、jetty-plugin に構成プロパティがあるようです。

  • testClassesDirectory : 生成されたテスト クラスを含むディレクトリ。
  • useTestClasspath : true の場合、テストの依存関係がランタイム クラスパスの最初に配置されます。

残念ながら、それらは機能しません。

私の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>
4

4 に答える 4

2

私はほぼ同じ問題を抱えており、カスタムweb.xml(jettyweb.xml)を使用して解決しましたmaven構成を参照してください

    <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 には、この春の構成があります

<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>

これにより、正しいパスに移動するはずです

于 2011-02-23T11:58:38.583 に答える
1

使った

<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>

私は春の設定を使用しました

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/myDS" />
</bean>
于 2012-08-21T13:29:37.060 に答える
1

著者と@thehpiの提案の両方を試しました。どちらも信頼できるものですが、試してみるといくつか問題がありました。私のセットアップは、次のMavenプロジェクトで、 との統合テストをmaven-failsafe-plugin使用し、コンテナにデータベースへの接続方法を伝えるために使用されるpersistence.xmlファイルでJPAを使用しました。

簡単に言うと、以下で詳しく説明するのは、JVM プロパティを使用して実際のコードに別の .xml を使用するように指示する方法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」というシステム プロパティに注意してください。

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>
于 2012-11-08T04:17:05.340 に答える
0

私は同じ問題に苦しんでいますが、useTestClasspathが機能していないように見える理由は、実際にはSpringにあると思います。

おそらく次のような構成があります。

<context:property-placeholder location="classpath*:**/*.properties"/>

最初の * を削除すると、この構成では同じプロパティ ファイルが異なる場所 (メインとテストの両方に存在する場合) から読み込まれるため、機能すると思います。* を削除すると、テストのみが読み込まれます。だからそれを

<context:property-placeholder location="classpath:**/*.properties"/>
于 2011-05-20T22:31:22.110 に答える