欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot整合websocket后运行测试类报错:javax.websocket.server.ServerContainer not available

程序员文章站 2023-09-07 18:06:18
springboot项目添加websocket依赖后运行测试类报如下错误: 解决办法:为SpringbootTest注解指定参数classes和webEnvironment 对于springboot如何创建ApplicationContext,可以参考博客:https://blog.csdn.net ......

springboot项目添加websocket依赖后运行测试类报如下错误:

  1 org.springframework.beans.factory.beancreationexception: error creating bean with name 'serverendpointexporter' defined in org.lwt.websocketservertestapplication: invocation of init method failed; nested exception is java.lang.illegalstateexception: javax.websocket.server.servercontainer not available
  2     at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1745) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
  3     at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:576) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
  4     at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:498) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
  5     at org.springframework.beans.factory.support.abstractbeanfactory.lambda$dogetbean$0(abstractbeanfactory.java:320) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
  6     at org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:222) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
  7     at org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:318) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
  8     at org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:199) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
  9     at org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:846) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
 10     at org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:863) ~[spring-context-5.1.4.release.jar:5.1.4.release]
 11     at org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:546) ~[spring-context-5.1.4.release.jar:5.1.4.release]
 12     at org.springframework.boot.springapplication.refresh(springapplication.java:775) ~[spring-boot-2.1.2.release.jar:2.1.2.release]
 13     at org.springframework.boot.springapplication.refreshcontext(springapplication.java:397) ~[spring-boot-2.1.2.release.jar:2.1.2.release]
 14     at org.springframework.boot.springapplication.run(springapplication.java:316) ~[spring-boot-2.1.2.release.jar:2.1.2.release]
 15     at org.springframework.boot.test.context.springbootcontextloader.loadcontext(springbootcontextloader.java:127) [spring-boot-test-2.1.2.release.jar:2.1.2.release]
 16     at org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontextinternal(defaultcacheawarecontextloaderdelegate.java:99) [spring-test-5.1.4.release.jar:5.1.4.release]
 17     at org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontext(defaultcacheawarecontextloaderdelegate.java:117) [spring-test-5.1.4.release.jar:5.1.4.release]
 18     at org.springframework.test.context.support.defaulttestcontext.getapplicationcontext(defaulttestcontext.java:108) [spring-test-5.1.4.release.jar:5.1.4.release]
 19     at org.springframework.test.context.web.servlettestexecutionlistener.setuprequestcontextifnecessary(servlettestexecutionlistener.java:190) [spring-test-5.1.4.release.jar:5.1.4.release]
 20     at org.springframework.test.context.web.servlettestexecutionlistener.preparetestinstance(servlettestexecutionlistener.java:132) [spring-test-5.1.4.release.jar:5.1.4.release]
 21     at org.springframework.test.context.testcontextmanager.preparetestinstance(testcontextmanager.java:246) [spring-test-5.1.4.release.jar:5.1.4.release]
 22     at org.springframework.test.context.junit4.springjunit4classrunner.createtest(springjunit4classrunner.java:227) [spring-test-5.1.4.release.jar:5.1.4.release]
 23     at org.springframework.test.context.junit4.springjunit4classrunner$1.runreflectivecall(springjunit4classrunner.java:289) [spring-test-5.1.4.release.jar:5.1.4.release]
 24     at org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) [junit-4.12.jar:4.12]
 25     at org.springframework.test.context.junit4.springjunit4classrunner.methodblock(springjunit4classrunner.java:291) [spring-test-5.1.4.release.jar:5.1.4.release]
 26     at org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:246) [spring-test-5.1.4.release.jar:5.1.4.release]
 27     at org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:97) [spring-test-5.1.4.release.jar:5.1.4.release]
 28     at org.junit.runners.parentrunner$3.run(parentrunner.java:290) [junit-4.12.jar:4.12]
 29     at org.junit.runners.parentrunner$1.schedule(parentrunner.java:71) [junit-4.12.jar:4.12]
 30     at org.junit.runners.parentrunner.runchildren(parentrunner.java:288) [junit-4.12.jar:4.12]
 31     at org.junit.runners.parentrunner.access$000(parentrunner.java:58) [junit-4.12.jar:4.12]
 32     at org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268) [junit-4.12.jar:4.12]
 33     at org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61) [spring-test-5.1.4.release.jar:5.1.4.release]
 34     at org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:70) [spring-test-5.1.4.release.jar:5.1.4.release]
 35     at org.junit.runners.parentrunner.run(parentrunner.java:363) [junit-4.12.jar:4.12]
 36     at org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:190) [spring-test-5.1.4.release.jar:5.1.4.release]
 37     at org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:86) [.cp/:na]
 38     at org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38) [.cp/:na]
 39     at org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:538) [.cp/:na]
 40     at org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:760) [.cp/:na]
 41     at org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:460) [.cp/:na]
 42     at org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:206) [.cp/:na]
 43 caused by: java.lang.illegalstateexception: javax.websocket.server.servercontainer not available
 44     at org.springframework.util.assert.state(assert.java:73) ~[spring-core-5.1.4.release.jar:5.1.4.release]
 45     at org.springframework.web.socket.server.standard.serverendpointexporter.afterpropertiesset(serverendpointexporter.java:106) ~[spring-websocket-5.1.4.release.jar:5.1.4.release]
 46     at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.invokeinitmethods(abstractautowirecapablebeanfactory.java:1804) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
 47     at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1741) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
 48     ... 40 common frames omitted
 49 
 50 2019-02-13 14:13:38.640 error 14060 --- [           main] o.s.test.context.testcontextmanager      : caught exception while allowing testexecutionlistener [org.springframework.test.context.web.servlettestexecutionlistener@1c93084c] to prepare test instance [org.lwt.websocketservertestapplicationtests@403132fc]
 51 
 52 java.lang.illegalstateexception: failed to load applicationcontext
 53     at org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontext(defaultcacheawarecontextloaderdelegate.java:125) ~[spring-test-5.1.4.release.jar:5.1.4.release]
 54     at org.springframework.test.context.support.defaulttestcontext.getapplicationcontext(defaulttestcontext.java:108) ~[spring-test-5.1.4.release.jar:5.1.4.release]
 55     at org.springframework.test.context.web.servlettestexecutionlistener.setuprequestcontextifnecessary(servlettestexecutionlistener.java:190) ~[spring-test-5.1.4.release.jar:5.1.4.release]
 56     at org.springframework.test.context.web.servlettestexecutionlistener.preparetestinstance(servlettestexecutionlistener.java:132) ~[spring-test-5.1.4.release.jar:5.1.4.release]
 57     at org.springframework.test.context.testcontextmanager.preparetestinstance(testcontextmanager.java:246) ~[spring-test-5.1.4.release.jar:5.1.4.release]
 58     at org.springframework.test.context.junit4.springjunit4classrunner.createtest(springjunit4classrunner.java:227) [spring-test-5.1.4.release.jar:5.1.4.release]
 59     at org.springframework.test.context.junit4.springjunit4classrunner$1.runreflectivecall(springjunit4classrunner.java:289) [spring-test-5.1.4.release.jar:5.1.4.release]
 60     at org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) [junit-4.12.jar:4.12]
 61     at org.springframework.test.context.junit4.springjunit4classrunner.methodblock(springjunit4classrunner.java:291) [spring-test-5.1.4.release.jar:5.1.4.release]
 62     at org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:246) [spring-test-5.1.4.release.jar:5.1.4.release]
 63     at org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:97) [spring-test-5.1.4.release.jar:5.1.4.release]
 64     at org.junit.runners.parentrunner$3.run(parentrunner.java:290) [junit-4.12.jar:4.12]
 65     at org.junit.runners.parentrunner$1.schedule(parentrunner.java:71) [junit-4.12.jar:4.12]
 66     at org.junit.runners.parentrunner.runchildren(parentrunner.java:288) [junit-4.12.jar:4.12]
 67     at org.junit.runners.parentrunner.access$000(parentrunner.java:58) [junit-4.12.jar:4.12]
 68     at org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268) [junit-4.12.jar:4.12]
 69     at org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61) [spring-test-5.1.4.release.jar:5.1.4.release]
 70     at org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:70) [spring-test-5.1.4.release.jar:5.1.4.release]
 71     at org.junit.runners.parentrunner.run(parentrunner.java:363) [junit-4.12.jar:4.12]
 72     at org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:190) [spring-test-5.1.4.release.jar:5.1.4.release]
 73     at org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:86) [.cp/:na]
 74     at org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38) [.cp/:na]
 75     at org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:538) [.cp/:na]
 76     at org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:760) [.cp/:na]
 77     at org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:460) [.cp/:na]
 78     at org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:206) [.cp/:na]
 79 caused by: org.springframework.beans.factory.beancreationexception: error creating bean with name 'serverendpointexporter' defined in org.lwt.websocketservertestapplication: invocation of init method failed; nested exception is java.lang.illegalstateexception: javax.websocket.server.servercontainer not available
 80     at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1745) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
 81     at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:576) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
 82     at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:498) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
 83     at org.springframework.beans.factory.support.abstractbeanfactory.lambda$dogetbean$0(abstractbeanfactory.java:320) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
 84     at org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:222) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
 85     at org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:318) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
 86     at org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:199) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
 87     at org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:846) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
 88     at org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:863) ~[spring-context-5.1.4.release.jar:5.1.4.release]
 89     at org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:546) ~[spring-context-5.1.4.release.jar:5.1.4.release]
 90     at org.springframework.boot.springapplication.refresh(springapplication.java:775) ~[spring-boot-2.1.2.release.jar:2.1.2.release]
 91     at org.springframework.boot.springapplication.refreshcontext(springapplication.java:397) ~[spring-boot-2.1.2.release.jar:2.1.2.release]
 92     at org.springframework.boot.springapplication.run(springapplication.java:316) ~[spring-boot-2.1.2.release.jar:2.1.2.release]
 93     at org.springframework.boot.test.context.springbootcontextloader.loadcontext(springbootcontextloader.java:127) ~[spring-boot-test-2.1.2.release.jar:2.1.2.release]
 94     at org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontextinternal(defaultcacheawarecontextloaderdelegate.java:99) ~[spring-test-5.1.4.release.jar:5.1.4.release]
 95     at org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontext(defaultcacheawarecontextloaderdelegate.java:117) ~[spring-test-5.1.4.release.jar:5.1.4.release]
 96     ... 25 common frames omitted
 97 caused by: java.lang.illegalstateexception: javax.websocket.server.servercontainer not available
 98     at org.springframework.util.assert.state(assert.java:73) ~[spring-core-5.1.4.release.jar:5.1.4.release]
 99     at org.springframework.web.socket.server.standard.serverendpointexporter.afterpropertiesset(serverendpointexporter.java:106) ~[spring-websocket-5.1.4.release.jar:5.1.4.release]
100     at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.invokeinitmethods(abstractautowirecapablebeanfactory.java:1804) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
101     at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1741) ~[spring-beans-5.1.4.release.jar:5.1.4.release]
102     ... 40 common frames omitted

解决办法:为springboottest注解指定参数classes和webenvironment

@springboottest(webenvironment = springboottest.webenvironment.defined_port)
因为websocket是servlet容器所支持的,所以需要加载servlet容器:
webenvironment参数为springboot指定applicationcontext类型。
webenvironment = springboottest.webenvironment.defined_port表示内嵌的服务器将会在一个随机的端口启动。
webenvironment主要有一下类型:
 1 enum webenvironment {
 2 
 3         /**
 4          * creates a {@link webapplicationcontext} with a mock servlet environment if
 5          * servlet apis are on the classpath, a {@link reactivewebapplicationcontext} if
 6          * spring webflux is on the classpath or a regular {@link applicationcontext}
 7          * otherwise.
 8          */
 9         mock(false),
10 
11         /**
12          * creates a web application context (reactive or servlet based) and sets a
13          * {@code server.port=0} {@link environment} property (which usually triggers
14          * listening on a random port). often used in conjunction with a
15          * {@link localserverport} injected field on the test.
16          */
17         random_port(true),
18 
19         /**
20          * creates a (reactive) web application context without defining any
21          * {@code server.port=0} {@link environment} property.
22          */
23         defined_port(true),
24 
25         /**
26          * creates an {@link applicationcontext} and sets
27          * {@link springapplication#setwebapplicationtype(webapplicationtype)} to
28          * {@link webapplicationtype#none}.
29          */
30         none(false);
31 
32         private final boolean embedded;
33 
34         webenvironment(boolean embedded) {
35             this.embedded = embedded;
36         }
37 
38         /**
39          * return if the environment uses an {@link servletwebserverapplicationcontext}.
40          * @return if an {@link servletwebserverapplicationcontext} is used.
41          */
42         public boolean isembedded() {
43             return this.embedded;
44         }
45 
46     }

对于springboot如何创建applicationcontext,可以参考博客:https://blog.csdn.net/lilongjiu/article/details/78045062。