美文网首页
Springboot 单元测试

Springboot 单元测试

作者: 走码人 | 来源:发表于2022-06-20 14:24 被阅读0次

IDE环境 Eclipse

导入依赖包

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

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
</dependency>

创建测试类

右键需要测试的类,选择Other


image.png

选择创建JUnit Test Case


image.png
image.png
选择创建目录,创建测试类
import org.junit.jupiter.api.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;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSON;
import com.jzfyjt.framework.core.base.controller.result.Result;
import com.jzfyjt.jzap.startup.JzapApiApplication;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { JzapApiApplication.class })
class SysUserServiceTest {
    @Autowired
    SysUserService sysUserService;

    @Test
    void test() {
        // success
        System.out.println("第一次获取");
        Result<Object> r = sysUserService.getSysUserById("0143");
        System.out.println(JSON.toJSONString(r));
    }

}

@SpringBootTest(classes = { JzapApiApplication.class })指定Springboot的启动类,此处至关重要

相关文章

网友评论

      本文标题:Springboot 单元测试

      本文链接:https://www.haomeiwen.com/subject/kcuhvrtx.html