(十)Spring Boot整合Junit

时间:2021-08-19
本文章向大家介绍(十)Spring Boot整合Junit,主要包括(十)Spring Boot整合Junit使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Spring Boot整合Junit

在Spring Boot项目中使用Junit进行单元测试UserService的方法

1.添加启动器依赖spring-boot-starter-test

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

2.编写测试类

mac环境 在UserService名称 上 按住command+shift+t 点击ok

package com.test.service;
import com.test.been.User;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
class UserServiceTest {
    @Autowired
    private UserService userService;

    @Test
    void queryById() {
       User user= userService.queryById(1);
        System.out.println("user"+user);
    }
    @Test
    void addUser() {
        User user=new User();
        user.setName("test");
        user.setUsername("test");
        user.setPassword("1234");
        user.setSex("男");
        user.setAge(14);
        user.setIsDelete(0);
        user.setPermission("允许");
        userService.addUser(user);
    }
}

3.启动测试类 查看结果

原文地址:https://www.cnblogs.com/nanao/p/15163440.html