websocket导致spring boot 项目单元测试启动失败的问题解决
在单元测试时,项目启动报错
Caused by: java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available at org.springframework.util.Assert.state(Assert.java:76) at org.springframework.web.socket.server.standard.ServerEndpointExporter.afterPropertiesSet(ServerEndpointExporter.java:107) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790) ... 41 more
单元测试类:
package com.mida.report;
import com.alibaba.fastjson.JSONObject;
import com.mida.MidaStart;
import com.mida.app.us.service.DocReportService;
import com.mida.app.us.vo.report.inputVo.DocReportPreservationVo;
import com.mida.app.us.vo.report.outputVo.DocReportSaveOrUpdateResultVo;
import com.mida.tool.common.result.ListResult;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.*;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MidaStart.class)
public class DocReportServiceImplTest {
@Test
public void postponeReport() {
}
@Test
public void getDocReport() {
}
@Test
public void getDocReportByStReportSoid() {
}
}
解决方案:
解决
在SpringBootTest增加webEnvironment 参数。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MidaStart.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DocReportServiceImplTest {
@Test
public void postponeReport() {
}
@Test
public void getDocReport() {
}
@Test
public void getDocReportByStReportSoid() {
}
}



