嵌入式码头用于自动化测试

时间:2013-12-16 08:24:23

标签: java spring testing jetty

我正在尝试使用嵌入式jetty进行Web服务的自动测试。但是,当我尝试访问该服务时,我一直收到错误404.

这是我的src / main / webapp / WEB-INF / web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Snafucator</display-name>

<session-config>
    <session-timeout>60</session-timeout>
</session-config>

<context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>Snafucator</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:snafucator.service.xml</param-value>
</context-param>

<servlet>
    <servlet-name>jaxws-servlet</servlet-name>
    <servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>jaxws-servlet</servlet-name>
    <url-pattern>/Snafucator</url-pattern>
</servlet-mapping>

我还希望我的src / test / resources在类路径上,因为它们包含DAO的设置和实现该服务的其他组件。服务所需的所有bean都在snafucator.service.xml中配置。这主要通过加载几个spring上下文文件来实现。这些是来自较低层的文件和snafucator.context.xml,它定义了服务绑定和实现:

<import resource="classpath*:setup.context.xml" />
<import resource="classpath*:core.context.xml" />
<import resource="classpath*:snafucator.context.xml" />

另外,我需要类路径上的目标/类,因为它包含snafucator.context.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://jax-ws.dev.java.net/spring/core
http://jax-ws.dev.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet
http://jax-ws.java.net/spring/servlet.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- enable autowiring by annotations -->
<context:annotation-config />

<wss:binding url="/Snafucator">
    <wss:service>
        <ws:service bean="#snafucatorWs" />
    </wss:service>
</wss:binding>
<bean id="snafucatorWs" class="com.snafu.SnafucatorWSImpl" />

这是我开始码头的方式:

    server = new Server(iPort);
WebAppContext root = new WebAppContext();
root.setExtraClasspath("target/classes/,src/test/resources/");
root.setDescriptor("src/main/webapp/WEB-INF/web.xml");
root.setResourceBase("src/main/webapp");    
root.setWar("src/main/webapp");
root.setContextPath("/");    
root.setServer(server);
root.setParentLoaderPriority(true);
server.setHandler(root);

System.out.println("Starting jetty server on port:" + iPort);
server.start();
System.out.println("Jetty server is started!");

当我访问"http://localhost:8082"时,我看到了WEB-INF和META-INF目录,因此Jetty正在运行。当我尝试"http://localhost:8082/Snafucator"时,我收到错误404,命令行显示“JAX-WS-Servlet Initialized”。当我访问"http://localhost:8082"下的任何其他网址时,我得到的格式略有不同404.所以我假设发生了一些WS处理。 我也正在开发一个Web服务器,该服务旨在成为其中的一部分。当我将snafucator.context.xml包含到该应用程序的上下文中并使用jetty eclipse插件启动应用程序时,一切正常。但对于自动化测试,我需要独立启动服务。

顺便说一下,我正在使用Jetty 7.6.14。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

抱歉抱歉。我使用没有WAR的嵌入式jetty。下面是Jetty的简单包装器。您可以通过上下文启动或使用

手动启动测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "path/testContext.xml" })

包装类

public class JettyWrapper extends Server implements ApplicationContextAware {
        private ApplicationContext appContext;

@Override
protected void doStart() throws Exception {

    final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    final GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext();
    webApplicationContext.setServletContext(context.getServletContext());
    webApplicationContext.setParent(appContext);
    context.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);
    webApplicationContext.refresh();
    DispatcherServlet dispatcherServlet = new DispatcherServlet();
    dispatcherServlet.setContextConfigLocation("classpath:servlet-context.xml");
    context.addServlet(new ServletHolder(dispatcherServlet), "/*");
    context.setContextPath("/");
    setHandler(context);
    super.doStart();
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.appContext = applicationContext;
}

在上下文中:

<bean id="webServer" class="package.JettyWrapper"
    destroy-method="stop" init-method="start">
    <property name="connectors">
        <list>
            <bean id="Connector" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
                <property name="port" value="${jetty-port}" />
            </bean>
        </list>
    </property>

希望有所帮助

答案 1 :(得分:0)

不幸的是,您的解决方案对我不起作用。我认为这不是我想要的。特别是,我想从现有的web.xml开始。无论如何,谢谢。但是,我找到了一个适用于我的解决方案:

  @Override
  public void start() throws Exception {
    // Set up the handler for the web application context.
    WebAppContext root = new WebAppContext();
    root.setExtraClasspath(extraClassPath);
    root.setDescriptor(new File(appDir, "WEB-INF/web.xml").getAbsolutePath());
    root.setWar(new File(appDir).getAbsolutePath());
    root.setContextPath(contextPath);
    root.setParentLoaderPriority(true);
    server.setHandler(root);
    server.start();
    System.out.println("Jetty server is started!");
  }

我认为唯一改变的是我没有设置resourceBase。现在效果很好。

干杯