2

我的目标是设置一个码头测试服务器并注入一个自定义 servlet 来测试我项目中的一些 REST 类。我能够使用 spring xml 启动服务器并针对该服务器运行测试。我遇到的问题有时是在服务器启动后,进程在运行测试之前停止。码头似乎没有进入背景。它每次都在我的电脑上工作。但是当我部署到我的 CI 服务器时,它不起作用。当我使用 VPN 时,它也不起作用。(奇怪的。)

服务器应该完成初始化,因为测试卡住了,我可以使用浏览器访问服务器。

这是我的弹簧上下文 xml:....

<bean id="servletHolder" class="org.eclipse.jetty.servlet.ServletHolder">
    <constructor-arg ref="courseApiServlet"/>
</bean>

<bean id="servletHandler" class="org.eclipse.jetty.servlet.ServletContextHandler"/>

<!-- Adding the servlet holders to the handlers -->
<bean id="servletHandlerSetter" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="servletHandler"/>
    <property name="targetMethod" value="addServlet"/>
    <property name="arguments">
        <list>
            <ref bean="servletHolder"/>
            <value>/*</value>
        </list>
    </property>
</bean>

<bean id="httpTestServer" class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="stop" depends-on="servletHandlerSetter">
    <property name="connectors">
        <list>
            <bean class="org.eclipse.jetty.server.nio.SelectChannelConnector">
                <property name="port" value="#{settings['webservice.server.port']}" />
            </bean>

        </list>
    </property>

    <property name="handler">
        <ref bean="servletHandler" />
    </property>
</bean>

运行最新的 Jetty 8.1.8 服务器和 Spring 3.1.3。任何的想法?

4

2 回答 2

1

我想到了。我的错。我的 REST 客户端连接到的测试 Web 服务器(码头)的 IP 地址设置为仅对我的本地主机可用的内部 IP 地址(不是本地主机)。这就是为什么当我在 VPN 或 CI 服务器上时无法开始测试的原因。

xml 实际上正在工作,我认为它比在单独的 ant 任务中启动码头更好。因为 spring 管理码头的生命周期。测试完成后,spring 会自动关闭 jetty。

于 2012-11-26T05:01:04.637 回答
0

如果您使用 Maven,您可以让jetty-maven-plugin启动和停止 Jetty 作为构建过程的一部分。你像往常一样创建你的 Spring 项目,将插件添加到你的 .pom 文件中。mvn jetty:run允许您运行未组装的 Web 应用程序,并且mvn jetty:run-war允许您运行 .war 文件。我想您真正想要的是让 Jetty 在预集成测试阶段开始并在集成后测试阶段停止(ref):

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <configuration>
    <scanIntervalSeconds>10</scanIntervalSeconds>
    <stopKey>foo</stopKey>
    <stopPort>9999</stopPort>
  </configuration>
  <executions>
    <execution>
      <id>start-jetty</id>
      <phase>pre-integration-test</phase>
      <goals>
        <goal>start</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>   
于 2012-11-25T09:01:38.923 回答