我用谷歌搜索了这个,但没有成功。我只是想看看如何在嵌入式 jetty 中加载我需要的所有服务以便能够在 JUnit 中进行测试的示例。所以,我的目标是这样的。
private String url = "SERVICE_URL";
@Before
public void before() {
// start jetty with all the services I need
}
@Test
public void shouldDoSthIWant() {
// invoke SERVICE_URL and test.
}
最佳答案
此处的示例应用程序展示了如何在简单的 Java 应用程序中使用嵌入式 jetty 运行 HttpInvoker。您应该能够调整该代码以使其在 junit 中工作。
http://code.google.com/p/jianwikis/wiki/SpringHttpRemotingWithEmbeddedJettyServer
从这里复制代码/配置的相关部分
Server server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(port);
Context context = new Context(server, "/", Context.SESSIONS);
DispatcherServlet dispatcherServlet = new DispatcherServlet();
dispatcherServlet.setContextConfigLocation("classpath:com/mycompany/config/DefaultServlet-servlet.xml");
ServletHolder servletHolder = new ServletHolder(dispatcherServlet);
context.addServlet(servletHolder, "/*")
DefaultServlet-servlet.xml
<!-- This default handler takes care of each of the services enumerated below -->
<bean id="defaultHandlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean id="helloService" class="com.mycompany.service.impl.HelloServiceImpl"/>
<!-- SpringHTTP Service Exposure -->
<bean name="/HelloService"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"
lazy-init="true">
<property name="service" ref="helloService" />
<property name="serviceInterface"
value="com.mycompany.service.iface.HelloService" />
</bean>
关于java - 如何在测试中加载 spring HTTPInvoker 到 jetty?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11504730/