Java Mybatis中使用Junit进行测试_程序员必备

写在前面

??MyBatis学习
?? 内容回顾
Java MyBatis的介绍及其执行原理
Java MyBatis配置详解
??今天我们进行 MyBatis框架使用Junit进行单元测试 的学习,感谢你的阅读,内容若有不当之处,希望大家多多指正,一起进步!!!
如果觉得博主文章还不错,可以??三连支持一下哦??
请添加图片描述

文章目录

Java Mybatis中使用Junit进行测试

Junit的引入

StudentMapper接口文件
在这里插入图片描述

先看我们的StudentMapper接口文件中有如上两个方法,我们要调用这两个方法,在前面的博客中,是通过在测试类的主方法中进行调用,可是我们有没有发现这样的问题。
在这里插入图片描述
当进行方法测试的时候,我们需要对其它方法调用的代码进行代码注释,虽然现在可能觉得没什么,但是如果接口文件中有许多方法呢?那么这样边注释,边切换方法就显得不那么友好了。
请添加图片描述

测试是保证代码健壮必不可少的环节,自己构建测试方法比较慢,而且也不规范,Java中提供了Junit的测试框架可以进行一键构建单元测试,所以在今天的博客中,我们引入Junit测试框架来对我们的方法进行测试。

在这里插入图片描述
有没有小伙伴发现,在我们创建Maven项目的时候,与main包包同层级下有一个test包包呢,其实这个包包就是专门用来我们进行类测试的。

Junit的使用

引入Junit依赖

pom.xml配置文件中添加Junit依赖。
在这里插入图片描述

生成测试类

第一步: 右键 要测试的类(接口),选择 Go To,选择Test 。或者使用快捷键 Ctrl + Shift + T

在这里插入图片描述
在这里插入图片描述

第二步: 选择我们要测试的方法

在这里插入图片描述

第三步: 生成测试类,路径与原来的类的相对路径一模一样

在这里插入图片描述

每个方法都带有 @Test 注解,表示要测试的方法

public class StudentMapperTest {

    @Test
    public void selectStudentById() {
    }

    @Test
    public void selectAllStudents() {
    }
}

Junit使用演示

在测试类的public void selectStudentById()方法体中编写代码逻辑

@Test
    public void selectStudentById() throws IOException {
        //1.读取全局配置文件
        String path = "mybatisConfig.xml";
        //2.通过Resources获取配置文件流
        InputStream resourceAsStream = Resources.getResourceAsStream(path);
        //3.创建会话工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //4.创建会话
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //5.通过代理模式创建代理类
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        //按学号查询学生信息
        Student student = mapper.selectStudentById(1);
        System.out.println(student);

        //关闭资源
        sqlSession.close();
    }

执行

在这里插入图片描述

此时如果以下几处显示绿色表示测试成功

在这里插入图片描述

@Before和@After注解

@Before注解作用的方法在Test注解作用的方法执行之前会执行,一般对于一些前置的资源等可以放在Before注解下的方法中。

@After注解作用的方法在Test注解作用的方法执行后才会执行,一般对于资源释放等操作可以放在After注解下的方法中。

比如我们要测试多个方法,这几个方法有同样的代码操作,此时我们可以考虑使用@Before注解或者@After注解,来实现代码的复用。

演示:
在生成的测试类中还有一个public void selectAllStudents()未测试,我们发现它和 public void selectStudentById()一样都要读取同一个全局配置文件和创建会话工厂,也要最后进行关闭资源的操作,这样我们就用@Before注解和@After注解来完成代码的编写。

public class StudentMapperTest {
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;

    @Before
    public void setUp() throws IOException {
        //1.读取全局配置文件
        String path = "mybatisConfig.xml";
        //2.通过Resources获取配置文件流
        InputStream resourceAsStream = Resources.getResourceAsStream(path);
        //3.创建会话工厂
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    }

    @Test
    public void selectStudentById() throws IOException {

        //4.创建会话
        sqlSession = sqlSessionFactory.openSession();
        //5.通过代理模式创建代理类
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
		//按学号查询学生信息
        Student student = mapper.selectStudentById(1);
        System.out.println(student);



    }

    @Test
    public void selectAllStudents() {
        //4.创建会话
        sqlSession = sqlSessionFactory.openSession();
        //5.通过代理模式创建代理类
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        //查询所有的学生信息
        List<Student> students = mapper.selectAllStudents();
        for (Student s : students) {
            System.out.println(s);
        }
    }

    @After
    public void setDown() {
        //关闭资源
        sqlSession.close();
    }
}

测试类中新增测试方法

假如接口中新增方法,并要测试,我们需要在测试类中手动添加测试方法。

在这里插入图片描述

首先写 @Test 注解,表明此方法要进行测试,然后写方法(与接口中方法保持一致),最后写代码逻辑。

student表中的数据。

mysql> select * from student;
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   1 | 赵雷  |   20 | 男   |
|   2 | 钱电  |   20 | 男   |
|   3 | 孙风  |   21 | 男   |
|   4 | 吴兰  |   18 | 女   |
|   5 | 孙兰  |   17 | 女   |
|   6 | 孙静  |   18 | 女   |
+-----+-------+------+------+
6 rows in set (0.00 sec)

测试类中新增测试方法

       @Test
    public void deleteStudentById() {
        sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        studentMapper.deleteStudentById(6);
        sqlSession.commit();//MyBatis中事务是默认手动提交
    }

执行,代码测试通过,查看数据库,SID为6的学生信息被删除

mysql> select * from student;
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   1 | 赵雷  |   20 | 男   |
|   2 | 钱电  |   20 | 男   |
|   3 | 孙风  |   21 | 男   |
|   4 | 吴兰  |   18 | 女   |
|   5 | 孙兰  |   17 | 女   |
+-----+-------+------+------+
5 rows in set (0.00 sec)

源码地址

源码地址:https://gitee.com/wang-yongsheng666/java-study2/tree/master/MyBatis

猜你喜欢

转载自blog.csdn.net/m0_67401228/article/details/123602973