springboot junit测试使用websocket时,它不起作用

时间:2017-08-18 14:07:00

标签: java junit spring-boot-test

1.当使用@ServerEndpoint时,Junit不起作用,websocket配置为列表,当@ServerEndpoint被推荐出来时,junit运行良好

@ServerEndpoint(value = "/websocket", configurator = SessionForWebSocket.class)
@Component
public class WebSocketBean {
    ....
}

2。 myjunit配置是

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = HdConsumerApplication.class)

@WebAppConfiguration
@Transactional
public class HdJunitTest {}

3.运行junit时,我收到错误:

2017-08-18 21:47:17 INFO  [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping  - Mapped "{[/env || /env.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-08-18 21:47:17 INFO  [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping  - Mapped "{[/actuator || /actuator.json],produces=[application/json]}" onto public org.springframework.hateoas.ResourceSupport org.springframework.boot.actuate.endpoint.mvc.HalJsonMvcEndpoint.links()
2017-08-18 21:47:17 INFO  [main] org.springframework.web.socket.server.standard.ServerEndpointExporter  - Registering @ServerEndpoint class: class net.huadong.tech.msg.WebSocketBean
2017-08-18 21:47:17 INFO  [main] org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer  - 
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-08-18 21:47:17 ERROR [main] org.springframework.boot.SpringApplication  - Application startup failed
java.lang.UnsupportedOperationException: MockServerContainer does not support addEndpoint(Class)
    at org.springframework.test.context.web.socket.MockServerContainer.addEndpoint(MockServerContainer.java:126)
    at org.springframework.web.socket.server.standard.ServerEndpointExporter.registerEndpoint(ServerEndpointExporter.java:145)
    at org.springframework.web.socket.server.standard.ServerEndpointExporter.registerEndpoints(ServerEndpointExporter.java:129)`enter code here`
    at org.springframework.web.socket.server.standard.ServerEndpointExporter.afterSingletonsInstantiated(ServerEndpointExporter.java:107)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:779)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371)

4.任何方法都可以帮助我,解决问题,或者当junit排除WebSocketBean时

1 个答案:

答案 0 :(得分:-1)

我遇到了同样的问题,但是当我添加以下代码时,它解决了问题。希望它可以帮到你。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ZplanApplication.class, 
                webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)