启动服务器进行集成测试失败

时间:2014-03-05 16:27:06

标签: java maven jetty jax-rs

我刚刚开始编写我的第一个Jax-RS Web服务,从昨天开始,我试图弄清楚如果我使用maven构建项目,我将如何自动启动测试服务器。如果我在不尝试执行某些测试的情况下运行以下命令,一切正常:

mvn clean install
export PORT=5000
java -cp target/classes:"target/dependency/*" net.avedo.spozz.Spozz

然后可以在localhost:5000 / services / users上获得该服务。但是,如果我尝试启动服务器并使用我在本文末尾附带的pom.xml自动运行测试,则会收到错误消息:

testHasUser(net.avedo.spozz.models.UserTest): Connection refused

这对应于这段代码:

HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://localhost:5000/services/users/1") ;
response = httpClient.execute(httpGet) ;

所以,我不确定这里有什么问题。我认为服务器是在错误的uri下启动的,但由于我没有在测试服务器启动的地方设置,我无法验证这一点。我需要知道在pom.xml文件中需要添加或更改的内容,以便运行测试服务器来运行项目的测试。这可能会有所帮助,这是我的项目结构:

   |-pom.xml
   |-src
   |---main
   |-----java
   |-------net
   |---------avedo
   |-----------spozz
   |-------------Spozz.java
   |-------------models
   |---------------User.java
   |-------------services
   |---------------UserResource.java
   |-----resources
   |-----webapp
   |-------index.html
   |-------WEB-INF
   |---------web.xml
   |---test
   |-----java
   |-------net
   |---------avedo
   |-----------spozz
   |-------------models
   |---------------UserTest.java
   |-----resources
   |-target
   |---classes
   |-----net
   |-------avedo
   |---------spozz
   |-----------Spozz.class
   |-----------models
   |-------------User.class
   |-----------services
   |-------------UserResource$1.class
   |-------------UserResource$2.class
   |-------------UserResource.class
   |---test-classes
   |-----net
   |-------avedo
   |---------spozz
   |-----------models
   |-------------UserTest.class

我的旧pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>net.avedo.spozz</groupId>
    <artifactId>Spozz-Service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Spozz REST Webservice</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- Jetty -->
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>7.6.0.v20120127</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>7.6.0.v20120127</version>
        </dependency>

        <!-- Jersey -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.8</version>
        </dependency>

        <!-- jUnit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

        <!-- Apache Commons -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.10</version>
                <configuration>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <stopKey>foo</stopKey>
                    <stopPort>5000</stopPort>
                </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>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals><goal>copy-dependencies</goal></goals>
                    </execution>
                </executions>
            </plugin>
    </plugins>

        <pluginManagement>
            <plugins>

                <!-- M2Eclipse Compatibility -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-dependency-plugin</artifactId>
                                        <versionRange>[2.4,)</versionRange>
                                        <goals>
                                            <goal>copy-dependencies</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <execute />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

\\编辑(1):

我更新了我的pom.xml以使用更新版本的码头,但现在我遇到了新的错误:

2014-03-06 11:51:29.126:WARN:oejuc.AbstractLifeCycle:FAILED org.eclipse.jetty.servlet.ServletHandler@66cbe14a:
java.lang.NoClassDefFoundError: org/eclipse/jetty/servlet/FilterMapping

此外,端口问题再次出现:

Error binding monitor port 8080: java.net.BindException: Address already in use
2014-03-06 11:51:24.909:INFO:oejs.Server:jetty-8.1.14.v20131031
2014-03-06 11:51:25.245:INFO:oejpw.PlusConfiguration:No Transaction manager found - if your webapp requires one, please configure one.
2014-03-06 11:51:26.839:WARN:oejsh.RequestLogHandler:!RequestLog
2014-03-06 11:51:26.847:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:28080

我更新的pom.xml

<project 
    xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>net.avedo.spozz</groupId>
    <artifactId>Spozz-Webservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jetty.version>9.1.0.v20131115</jetty.version>
        <jersey.version>1.8</jersey.version>
        <junit.version>4.11</junit.version>
        <apache.commons.version>1.3.2</apache.commons.version>
        <apache.http.version>4.3.2</apache.http.version>
        <jsp.version>2.5</jsp.version>
        <maven.compiler.plugin.version>2.5.1</maven.compiler.plugin.version>
        <sql.maven.plugin.version>1.5</sql.maven.plugin.version>
        <postgresql.jdbc.version>9.1-901.jdbc4</postgresql.jdbc.version>
    </properties>

    <dependencies>
        <!-- Jetty -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-jsp</artifactId>
            <version>${jetty.version}</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <artifactId>org.eclipse.jdt.core</artifactId>
                    <groupId>org.eclipse.jetty.orbit</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Jersey -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <!-- jUnit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- Apache Commons -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${apache.commons.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>${apache.http.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- PostgreSQL -->
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql.jdbc.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.14.v20131031</version>
                <configuration>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <stopKey>foo</stopKey>
                    <stopPort>8080</stopPort>
                    <stopWait>10</stopWait>

                    <connectors>
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                            <port>28080</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
                <executions>
                    <execution>
                        <id>start-jetty</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <scanIntervalSeconds>0</scanIntervalSeconds>
                            <daemon>true</daemon>
                        </configuration>
                    </execution>
                    <execution>
                        <id>stop-jetty</id>
                        <phase>test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sql-maven-plugin</artifactId>
                <version>${sql.maven.plugin.version}</version>

                <dependencies>
                    <!-- Specify the dependent jdbc driver here -->
                    <dependency>
                        <groupId>postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${postgresql.jdbc.version}</version>
                    </dependency>
                </dependencies>

                <!-- Common configuration shared by all executions -->
                <configuration>
                    <driver>org.postgresql.Driver</driver>
                    <url>jdbc:postgresql://localhost:5432:spozz_db</url>
                    <username>postgres</username>
                    <password>root</password>
                    <!-- You can comment out username/password configurations and have 
                        maven to look them up in your settings.xml using ${settingsKey} -->
                    <settingsKey>sensibleKey</settingsKey>
                    <!-- All executions are ignored if -Dmaven.test.skip=true -->
                    <skip>${maven.test.skip}</skip>
                </configuration>

                <executions>
                    <execution>
                        <id>drop-schema-before-test-if-any</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <!-- Need another database to drop the targeted one -->
                            <url>jdbc:postgresql://localhost:5432:postgres</url>
                            <autocommit>true</autocommit>
                            <sqlCommand>DROP SCHEMA spozz CASCADE</sqlCommand>
                            <!-- Ignore error when database is not available -->
                            <onError>continue</onError>
                        </configuration>
                    </execution>

                    <execution>
                        <id>create-schema</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <autocommit>true</autocommit>
                            <srcFiles>
                                <srcFile>src/main/sql/spozz-schema.sql</srcFile>
                            </srcFiles>
                        </configuration>
                    </execution>

                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>dbunit-maven-plugin</artifactId>
                <version>1.0-beta-3</version>

                <dependencies>
                    <!-- Specify the dependent jdbc driver here -->
                    <dependency>
                        <groupId>postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${postgresql.jdbc.version}</version>
                    </dependency>
                </dependencies>

                <!-- Common configuration shared by all executions -->
                <configuration>
                    <driver>org.postgresql.Driver</driver>
                    <url>jdbc:postgresql://localhost:5432:spozz_db</url>
                    <username>postgres</username>
                    <password>root</password>
                    <!-- You can comment out username/password configurations and have maven 
                        to look them up in your settings.xml using ${settingsKey} -->
                    <settingsKey>sensibleKey</settingsKey>
                    <!-- All executions are ignored if -Dmaven.test.skip=true -->
                    <skip>${maven.test.skip}</skip>
                </configuration>

                <executions>
                    <execution>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>operation</goal>
                        </goals>
                        <!-- Specific configurations -->
                        <configuration>
                            <type>CLEAN_INSERT</type>
                            <src>src/test/resources/spozz_db_testdata.xml</src>
                        </configuration>
                    </execution>
                </executions>

            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals><goal>copy-dependencies</goal></goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <pluginManagement>
            <plugins>

                <!-- M2Eclipse Compatibility -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-dependency-plugin</artifactId>
                                        <versionRange>[2.4,)</versionRange>
                                        <goals>
                                            <goal>copy-dependencies</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <execute />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

1 个答案:

答案 0 :(得分:0)

如果这些是在test阶段执行的常规测试,那么您在pre-integration-test中启动码头的事实就是您的问题。您应该在test-compile阶段运行它。