【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);
}
}
分类:
Spring Boot
标签:
springboot
, junit
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律