Junit和spring websockets - 在每次测试后我得到一个奇怪的例外

时间:2014-05-27 15:15:03

标签: java spring junit websocket spring-websocket

当我的jUnit测试结束时,我收到以下异常:

7 May 2014 18:06:29  INFO GenericApplicationContext - Closing  org.springframework.context.support.GenericApplicationContext@7b0133c2: startup date [Tue May 27      18:06:09 IDT 2014]; root of context hierarchy
27 May 2014 18:06:29  INFO DefaultLifecycleProcessor - Stopping beans in phase 2147483647
27 May 2014 18:06:29  INFO NettyTcpClient - CLOSED: [id: 0x09b0bb52, /127.0.0.1:56869 :>  /127.0.0.1:61613]
27 May 2014 18:06:31 ERROR StompBrokerRelayMessageHandler - Error while shutting down TCP client
java.util.concurrent.TimeoutException
at org.springframework.messaging.tcp.reactor.AbstractPromiseToListenableFutureAdapter.get(AbstractPromiseToListenableFutureAdapter.java:84)
at org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler.stopInternal(StompBrokerRelayMessageHandler.java:377)
at org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler.stop(AbstractBrokerMessageHandler.java:150)
at org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler.stop(AbstractBrokerMessageHandler.java:164)
at org.springframework.context.support.DefaultLifecycleProcessor.doStop(DefaultLifecycleProcessor.java:229)
at org.springframework.context.support.DefaultLifecycleProcessor.access$300(DefaultLifecycleProcessor.java:51)
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.stop(DefaultLifecycleProcessor.java:363)
at org.springframework.context.support.DefaultLifecycleProcessor.stopBeans(DefaultLifecycleProcessor.java:202)
at org.springframework.context.support.DefaultLifecycleProcessor.onClose(DefaultLifecycleProcessor.java:118)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:888)
at org.springframework.context.support.AbstractApplicationContext$1.run(AbstractApplicationContext.java:809)
27 May 2014 18:06:31  INFO ThreadPoolTaskExecutor - Shutting down ExecutorService 'brokerChannelExecutor'
27 May 2014 18:06:31  INFO ThreadPoolTaskScheduler - Shutting down ExecutorService 'messageBrokerSockJsTaskScheduler'
27 May 2014 18:06:31  INFO ThreadPoolTaskExecutor - Shutting down ExecutorService 'clientOutboundChannelExecutor'
27 May 2014 18:06:31  INFO ThreadPoolTaskExecutor - Shutting down ExecutorService 'clientInboundChannelExecutor'
27 May 2014 18:06:31  INFO EhCacheManagerFactoryBean - Shutting down EhCache CacheManager
27 May 2014 18:06:31  INFO LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default'
27 May 2014 18:06:31  INFO DefaultContextLoadTimeWeaver - Removing all registered transformers for class loader: sun.misc.Launcher$AppClassLoader

任何想法如何预防?

P.S - 测试成功但我真的很讨厌看到堆栈跟踪

编辑 - 我的考试班:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/application-context.xml" })
public class EmptyTest{
  /**
  * Test (empty)
  */
  @Test()
  public void emptyTest() {

  assertTrue(true);

  }

}

这是我的经纪人配置文件:

@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {

config.enableStompBrokerRelay(
                  "/topic",
                  "/queue/");

config.setApplicationDestinationPrefixes("/app");
}


@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {

registry.addEndpoint(
             "/wsdemo").withSockJS();
}

}

1 个答案:

答案 0 :(得分:1)

正如我所看到的,你有几个选择,具体取决于你现在在测试中想要达到的目标,以及你将来可能想要做的事情。

1)如果您的测试根本不需要websocket配置,请将其更改为指向不包含WebSocketConfig的自定义上下文配置。

2)如果您的测试需要websocket配置,但您不需要代理中继(我不明白为什么在测试中需要它),您可以添加另一个用于测试的配置{{1而不是registry.enableSimpleBroker("/topic", "/queue/")。然后将没有与代理的TCP连接。这种方法的明显缺点是您没有测试您的实际配置,并且您正在复制目标前缀。

3)为您的测试运行嵌入式STOMP代理。我不是100%确定存在这样的事情 - 我知道ActiveMQ有STOMP支持和running inside a VM的一些支持,但我没有用STOMP尝试过。如果可能的话,这种方法的优点是你的测试将测试非常接近实际代码的东西。

4)您可以自定义STOMP代理中继,以便您可以完全控制应用程序从代理接收的内容。您可以通过添加扩展StompBrokerRelayMessageHandler的配置类来自定义管理与中继代理的连接的DelegatingWebSocketMessageBrokerConfiguration,以覆盖enableStompBrokerRelay方法。例如,您可以将它使用的TCP客户端设置为您自己的stompBrokerRelayMessageHandler()实现。下面是一个示例TCP客户端,它什么都不做,即使Handler认为它已连接,但无法接收或发送消息。

TcpOperations

请注意,@Override public AbstractBrokerMessageHandler stompBrokerRelayMessageHandler() { AbstractBrokerMessageHandler handler = super.stompBrokerRelayMessageHandler(); if (handler instanceof StompBrokerRelayMessageHandler) { StompBrokerRelayMessageHandler stompHandler = (StompBrokerRelayMessageHandler) handler; stompHandler.setTcpClient(new TcpOperations<byte[]>() { @Override public ListenableFuture<Void> connect(TcpConnectionHandler<byte[]> connectionHandler) { return new CompletedListenableFuture<>(null); } @Override public ListenableFuture<Void> connect(TcpConnectionHandler<byte[]> connectionHandler, ReconnectStrategy reconnectStrategy) { return new CompletedListenableFuture<>(null); } @Override public ListenableFuture<Void> shutdown() { return new CompletedListenableFuture<>(null); } }); } return handler; } 只是CompletedListenableFuture的一个实现,它在构造之后完成,并立即调用传递给ListenableFuture的任何回调,并将值传递给构造函数。

这里的要点是您可以轻松自定义代理中继组件的确切行为,以便您可以在测试中更好地控制它们。我不知道有任何内置的支持来使这种测试更容易,但是后来的websocket支持仍然很新。我建议您查看Rossen Stoyanchev的优秀示例项目spring-websocket-portfolio,如果您还没有这样做,因为它包含了几个如何在不同级别测试websocket配置的示例(只有一个控制器,加载完整上下文,运行嵌入式服务器,...)。希望这对于决定如何测试应用程序以及您可能需要自定义以执行此操作也很有帮助。