使用Spring / Jetty(或Tomcat)/ Maven进行REST测试的集成测试

我想用HTTP请求测试我们的REST服务(有点黑盒测试).然而,经过几个小时的谷歌搜索和尝试不同的配置,我仍然无法正确启动Jetty.这是我当前的配置(我尝试了多个不同的版本):

的pom.xml

        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <junitArtifactName>junit:junit</junitArtifactName>
                <excludes>
                    <exclude>**/*_Roo_*</exclude>
                    <exclude>**/*Util*</exclude>
                    <exclude>**/*IT*</exclude>
                </excludes>
                <forkMode>once</forkMode>
                <argLine>-javaagent:'${settings.localRepository}/org/springframework/spring-instrument/${org.springframework-version}/spring-instrument-${org.springframework-version}.jar' -javaagent:'${settings.localRepository}/org/aspectj/aspectjweaver/${org.aspectj-version}/aspectjweaver-${org.aspectj-version}.jar'</argLine>
                <useSystemClassLoader>true</useSystemClassLoader> 
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <forkMode>once</forkMode>
                <argLine>-javaagent:'${settings.localRepository}/org/springframework/spring-instrument/${org.springframework-version}/spring-instrument-${org.springframework-version}.jar' -javaagent:'${settings.localRepository}/org/aspectj/aspectjweaver/${org.aspectj-version}/aspectjweaver-${org.aspectj-version}.jar'</argLine>
                <useSystemClassLoader>false</useSystemClassLoader>
            </configuration>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
                <execution>
                    <id>verify</id>
                    <goals>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>  
            <groupId>org.mortbay.jetty</groupId>  
            <artifactId>maven-jetty-plugin</artifactId>  
            <version>6.1.10</version>  
            <configuration>  
                <scanintervalseconds>10</scanintervalseconds>  
                <stopkey>foo</stopkey>  
                <stopport>9999</stopport>  
                <contextpath>/${project.artifactId}</contextpath>  
                <connectors>  
                    <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">  
                        <port>9090</port>  
                    </connector>  
                </connectors>  
            </configuration>  
            <executions>  
                <execution>  
                    <id>start-jetty</id>  
                    <phase>pre-integration-test</phase>  
                    <goals>  
                        <goal>run</goal>  
                    </goals>  
                    <configuration>  
                        <scanintervalseconds>0</scanintervalseconds>  
                        <daemon>true</daemon>  
                    </configuration>  
                </execution>  
                <execution>  
                    <id>stop-jetty</id>  
                    <phase>post-integration-test</phase>  
                    <goals>  
                        <goal>stop</goal>  
                    </goals>  
                </execution>  
            </executions>  
        </plugin>
        ...

orm.xml中

<!-- <context:load-time-weaver/> -->
<context:load-time-weaver weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
<context:spring-configured/>
<context:annotation-config />
<context:component-scan base-package="com.example.jump.domain">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
    <tx:annotation-driven transaction-manager="transactionManager" />

现在,我得到了例外:

> java.lang.IllegalStateException:ClassLoader [org.mortbay.jetty.webapp.WebAppClassLoader]不提供’addTransformer(ClassFileTransformer)’方法.指定自定义LoadTimeWeaver或使用Spring的代理启动Java虚拟机:-javaagent:org.springframework.instrument.jar

我理解异常,但我无法解决它.我还尝试了其他几种方法,比如通过MAVEN_OPTS提供javaagent或尝试将VM args设置为Jetty.问题是谷歌为不同的版本提供了一堆“解决方案”,但没有一个正在运行.

我目前非常沮丧的是,在Spring中很难实现像REST服务测试这样的常见场景.因此,如果您足够聪明地找到正确的配置,请以一种甚至猴子可以配置它的方式与我分享,我保证如果我们见过啤酒在我身上.

以下是我使用的版本:

<properties>
    <java-version>1.6</java-version>
    <org.springframework-version>3.0.5.RELEASE</org.springframework-version>
    <org.aspectj-version>1.6.9</org.aspectj-version>
    <org.slf4j-version>1.5.10</org.slf4j-version>
    <redis.version>1.0.0.BUILD-SNAPSHOT</redis.version>
    <org.jboss.hibernate.version>3.6.0.Final</org.jboss.hibernate.version>
    <net.sf.ehcache.version>2.3.1</net.sf.ehcache.version>
</properties>

Maven 2.21

如果需要,我很乐意提供更多信息.就像我说的每一个帮助都赞赏.

最佳答案
jetty-maven-plugin现在支持run-forked目标,您可以在其中传递JVM参数.比货物或MAVEN_OPTS的tomcat容易得多.

转载注明原文:使用Spring / Jetty(或Tomcat)/ Maven进行REST测试的集成测试 - 代码日志