若依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 文件

import com.ruoyi.RuoYiApplication;
import com.ruoyi.activity.domain.Activity;
import com.ruoyi.activity.mapper.ActivityMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = RuoYiApplication.class)
public class MainTests {

    @Autowired
    private ActivityMapper activityMapper;

    @Test
    public void testSelectActivityById() {
        Activity activity = activityMapper.selectActivityById(1);
        System.out.println(activity);
    }

    @Test
    public void testSelectActivityList() {
        List<Activity> activityList = activityMapper.selectActivityList(new Activity());
        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)
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot项目中,可以通过创建一个新的单元测试来进行单元测试。在这个中,你需要添加`@SpringBootTest`注解来表示当前单元测试运行在Spring Boot环境中。同时,你还需要导入相关的依赖和,例如`org.junit.jupiter.api.Assertions`和`org.junit.jupiter.api.Test`。在这个单元测试中,你可以编写测试方法来测试你的业务代码。例如,你可以使用`@Test`注解来标记一个测试方法,并在方法中编写测试逻辑。在测试方法中,你可以使用断言来验证你的代码的预期行为。例如,你可以使用`Assertions.assertNotNull()`来验证某个对象不为空。以下是一个示例的单元测试: ```java import com.example.demo.mapper.UserMapper; import com.example.demo.model.UserInfo; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class UserMapperTest { @Autowired private UserMapper userMapper; @Test void getUserById() { UserInfo userInfo = userMapper.getUserById(1); Assertions.assertNotNull(userInfo); } } ``` 在这个示例中,我们使用`@Autowired`注解来自动注入`UserMapper`对象,并在`getUserById()`方法中调用`userMapper.getUserById()`方法来获取用户信息。然后,我们使用`Assertions.assertNotNull()`来验证获取到的用户信息不为空。这样,我们就可以通过运行这个单元测试测试`UserMapper`的功能。 #### 引用[.reference_title] - *1* *2* *3* [【SpringBootSpringBoot单元测试](https://blog.csdn.net/m0_46468731/article/details/125969337)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值