springboot项目添加websocket依赖后运行测试类报如下错误:
: Error creating bean with name 'serverEndpointExporter' defined in : Invocation of init method failed; nested exception is : not available
at (:1745) ~[spring-beans-5.1.:5.1.]
at (:576) ~[spring-beans-5.1.:5.1.]
at (:498) ~[spring-beans-5.1.:5.1.]
at $doGetBean$0(:320) ~[spring-beans-5.1.:5.1.]
at (:222) ~[spring-beans-5.1.:5.1.]
at (:318) ~[spring-beans-5.1.:5.1.]
at (:199) ~[spring-beans-5.1.:5.1.]
at (:846) ~[spring-beans-5.1.:5.1.]
at (:863) ~[spring-context-5.1.:5.1.]
at (:546) ~[spring-context-5.1.:5.1.]
at (:775) ~[spring-boot-2.1.:2.1.]
at (:397) ~[spring-boot-2.1.:2.1.]
at (:316) ~[spring-boot-2.1.:2.1.]
at (:127) [spring-boot-test-2.1.:2.1.]
at (:99) [spring-test-5.1.:5.1.]
at (:117) [spring-test-5.1.:5.1.]
at (:108) [spring-test-5.1.:5.1.]
at (:190) [spring-test-5.1.:5.1.]
at (:132) [spring-test-5.1.:5.1.]
at (:246) [spring-test-5.1.:5.1.]
at .junit4.(:227) [spring-test-5.1.:5.1.]
at .junit4.SpringJUnit4ClassRunner$(:289) [spring-test-5.1.:5.1.]
at (:12) [junit-4.:4.12]
at .junit4.(:291) [spring-test-5.1.:5.1.]
at .junit4.(:246) [spring-test-5.1.:5.1.]
at .junit4.(:97) [spring-test-5.1.:5.1.]
at $(:290) [junit-4.:4.12]
at $(:71) [junit-4.:4.12]
at (:288) [junit-4.:4.12]
at $000(:58) [junit-4.:4.12]
at $(:268) [junit-4.:4.12]
at .(:61) [spring-test-5.1.:5.1.]
at .(:70) [spring-test-5.1.:5.1.]
at (:363) [junit-4.:4.12]
at .junit4.(:190) [spring-test-5.1.:5.1.]
at ..(:86) [.cp/:na]
at (:38) [.cp/:na]
at (:538) [.cp/:na]
at (:760) [.cp/:na]
at (:460) [.cp/:na]
at (:206) [.cp/:na]
Caused by: : not available
at (:73) ~[spring-core-5.1.:5.1.]
at (:106) ~[spring-websocket-5.1.:5.1.]
at (:1804) ~[spring-beans-5.1.:5.1.]
at (:1741) ~[spring-beans-5.1.:5.1.]
... 40 common frames omitted
2019-02-13 14:13:38.640 ERROR 14060 --- [ main] : Caught exception while allowing TestExecutionListener [@1c93084c] to prepare test instance [@403132fc]
: Failed to load ApplicationContext
at (:125) ~[spring-test-5.1.:5.1.]
at (:108) ~[spring-test-5.1.:5.1.]
at (:190) ~[spring-test-5.1.:5.1.]
at (:132) ~[spring-test-5.1.:5.1.]
at (:246) ~[spring-test-5.1.:5.1.]
at .junit4.(:227) [spring-test-5.1.:5.1.]
at .junit4.SpringJUnit4ClassRunner$(:289) [spring-test-5.1.:5.1.]
at (:12) [junit-4.:4.12]
at .junit4.(:291) [spring-test-5.1.:5.1.]
at .junit4.(:246) [spring-test-5.1.:5.1.]
at .junit4.(:97) [spring-test-5.1.:5.1.]
at $(:290) [junit-4.:4.12]
at $(:71) [junit-4.:4.12]
at (:288) [junit-4.:4.12]
at $000(:58) [junit-4.:4.12]
at $(:268) [junit-4.:4.12]
at .(:61) [spring-test-5.1.:5.1.]
at .(:70) [spring-test-5.1.:5.1.]
at (:363) [junit-4.:4.12]
at .junit4.(:190) [spring-test-5.1.:5.1.]
at ..(:86) [.cp/:na]
at (:38) [.cp/:na]
at (:538) [.cp/:na]
at (:760) [.cp/:na]
at (:460) [.cp/:na]
at (:206) [.cp/:na]
Caused by: : Error creating bean with name 'serverEndpointExporter' defined in : Invocation of init method failed; nested exception is : not available
at (:1745) ~[spring-beans-5.1.:5.1.]
at (:576) ~[spring-beans-5.1.:5.1.]
at (:498) ~[spring-beans-5.1.:5.1.]
at $doGetBean$0(:320) ~[spring-beans-5.1.:5.1.]
at (:222) ~[spring-beans-5.1.:5.1.]
at (:318) ~[spring-beans-5.1.:5.1.]
at (:199) ~[spring-beans-5.1.:5.1.]
at (:846) ~[spring-beans-5.1.:5.1.]
at (:863) ~[spring-context-5.1.:5.1.]
at (:546) ~[spring-context-5.1.:5.1.]
at (:775) ~[spring-boot-2.1.:2.1.]
at (:397) ~[spring-boot-2.1.:2.1.]
at (:316) ~[spring-boot-2.1.:2.1.]
at (:127) ~[spring-boot-test-2.1.:2.1.]
at (:99) ~[spring-test-5.1.:5.1.]
at (:117) ~[spring-test-5.1.:5.1.]
... 25 common frames omitted
Caused by: : not available
at (:73) ~[spring-core-5.1.:5.1.]
at (:106) ~[spring-websocket-5.1.:5.1.]
at (:1804) ~[spring-beans-5.1.:5.1.]
at (:1741) ~[spring-beans-5.1.:5.1.]
... 40 common frames omitted
解决办法:为SpringbootTest注解指定参数classes和webEnvironment
@SpringBootTest(classes = , webEnvironment = .DEFINED_PORT)
因为WebSocket是servlet容器所支持的,所以需要加载servlet容器:
webEnvironment参数为springboot指定ApplicationContext类型。
webEnvironment = .DEFINED_PORT表示内嵌的服务器将会在一个随机的端口启动。
webEnvironment 主要的值可以参考SpringbootTest源码:
/**
* An enumeration web environment modes.
*/
enum WebEnvironment {
/**
* Creates a {@link WebApplicationContext} with a mock servlet environment if
* servlet APIs are on the classpath, a {@link ReactiveWebApplicationContext} if
* Spring WebFlux is on the classpath or a regular {@link ApplicationContext}
* otherwise.
*/
MOCK(false),
/**
* Creates a web application context (reactive or servlet based) and sets a
* {@code =0} {@link Environment} property (which usually triggers
* listening on a random port). Often used in conjunction with a
* {@link LocalServerPort} injected field on the test.
*/
RANDOM_PORT(true),
/**
* Creates a (reactive) web application context without defining any
* {@code =0} {@link Environment} property.
*/
DEFINED_PORT(true),
/**
* Creates an {@link ApplicationContext} and sets
* {@link SpringApplication#setWebApplicationType(WebApplicationType)} to
* {@link WebApplicationType#NONE}.
*/
NONE(false);
private final boolean embedded;
WebEnvironment(boolean embedded) {
= embedded;
}
/**
* Return if the environment uses an {@link ServletWebServerApplicationContext}.
* @return if an {@link ServletWebServerApplicationContext} is used.
*/
public boolean isEmbedded() {
return ;
}
}
对于springboot如何创建ApplicationContext,可以参考博客:/lilongjiu/article/details/78045062。