Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

springboot junit test when use websocket , it doesn't work

1.When using @ServerEndpoint, Junit does not work,the websocket config as list,when @ServerEndpoint is commend out, junit works well

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

2. myjunit config is

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

@WebAppConfiguration
@Transactional
public class HdJunitTest {}

3.when run junit,i got the error:

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.is anymethod can help me,fix the matter ,or when junit exclude WebSocketBean

like image 966
lightning_16 Avatar asked Feb 16 '26 06:02

lightning_16


1 Answers

I had the same problem, but when I added the following code it solved the problem. Hope it helps you.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ZplanApplication.class, 
                webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
like image 118
红尘忘 Avatar answered Feb 17 '26 20:02

红尘忘



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!