0


若依SpringBoot添加单元测试类及测试类启动报错

若依SpringBoot添加单元测试类及测试类启动报错

一、添加测试类的依赖

在admin 模块中添加单元测试
将以下依赖添加到 admin 的 pom.xml 中

<!--测试类--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><!--测试类--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><scope>test</scope></dependency><!--测试类--><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><scope>test</scope></dependency>

二、编写测试类

在 src 目录下创建 test.java.MainTests 文件

importcom.ruoyi.RuoYiApplication;importcom.ruoyi.activity.domain.Activity;importcom.ruoyi.activity.mapper.ActivityMapper;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;importjava.util.List;@RunWith(SpringRunner.class)@SpringBootTest(classes =RuoYiApplication.class)publicclassMainTests{@AutowiredprivateActivityMapper activityMapper;@TestpublicvoidtestSelectActivityById(){Activity activity = activityMapper.selectActivityById(1);System.out.println(activity);}@TestpublicvoidtestSelectActivityList(){List<Activity> activityList = activityMapper.selectActivityList(newActivity());System.out.println(activityList);}}

三、Spring Boot 加入websocket后,单元测试启动报错(javax.websocket.server.ServerContainer not available)

错误提示:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘serverEndpointExporter’ defined in class path resource [com/zou/sell/config/WebSocketConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available

解决方案:
在springbootTest注解加入 webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,我们在测试使用 websocket的时候需要启动一个完整的服务器,而使用这个注解就是说每次测试都会选用一个随即可用的端口模拟启动一个完整的服务器

@SpringBootTest(classes =RuoYiApplication.class,webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)

本文转载自: https://blog.csdn.net/m0_56090275/article/details/134716983
版权归原作者 天天++ 所有, 如有侵权,请联系我们删除。

“若依SpringBoot添加单元测试类及测试类启动报错”的评论:

还没有评论