【junit】SpringBoot集成junit测试

引入依赖

<!--        可以省略get set方法-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>

测试类

boot项目会有一个test目录,下面这个类可以用于单元测试

图片

上代码!

// 用到自动注入的话,需要添加注解:@RunWith(SpringRunner.class)



package com.yibing;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yibing.entity.Common;
import com.yibing.entity.Course;
import com.yibing.mapper.CommonMapper;
import com.yibing.mapper.CourseMapper;
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 java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
class ShardingJdbcStudyApplicationTests {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CommonMapper commonMapper;
    @Test
    public void insertCourse() {
        for (int i = 0; i < 100; i++) {
            Course course = new Course();
            course.setCname("yibing:" + i);
            course.setCstatus("ok");
            course.setUserId(Long.valueOf(i + ""));
            courseMapper.insert(course);
        }
    }
    @Test
    public void getCourse() {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", 0L);
        //wrapper.eq("cid","00730728515162341377");
        List<Course> list = courseMapper.selectList(wrapper);
        for (Course c : list) {
            System.out.println(c);
        }
    }
    @Test
    public void insertCommon(){
        Common common = new Common();
        common.setId(1526699439887036419L);
        common.setMsg("hello sharding jdbc");
        commonMapper.insert(common);
    }
    @Test
    public void delCommon(){
        QueryWrapper<Common>  wrapper = new QueryWrapper<>();
        wrapper.eq("id",1526699439887036419L);
        commonMapper.delete(wrapper);
    }
}

posted @   天门道人  阅读(156)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示