开发喵星球

若依框架如何进行单元测试(十二)

如何在若依框架下进行单元测试,下面教大家如何写一个简单的单元测试案例。

1、导入单元测试依赖包

ruoyi-admin模块中的pom.xml中添加以下依赖配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.6.1</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>

2、新建单元测试类

import com.ruoyi.RuoYiApplication;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = RuoYiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class JavaTest {

    @Autowired
    private ISysUserService sysUserService;

    @Test
    public void test() {
        SysUser sysUser = sysUserService.selectUserById(1L);
        System.out.println(sysUser);
    }
}

3、运行测试类结果

image-20231017010838140

   
分类:Java/OOP 作者:无限繁荣, 吴蓉 发表于:2023-10-17 09:11:42 阅读量:208
<<   >>


powered by kaifamiao