1 单元测试
单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证。

JUnit 是 Java 社区中知名度最高的单元测试工具。

2 单元测试配置
步骤 1:添加 JUnit 的 jar 包

由于我们使用的是 JUnit,我们要确保它的 jar 包可以被应用使用。

为此,需要将 jar 包添加到应用的类路径中。

如果使用 Maven 来构建项目,则需将下面的依赖代码置于 pom.xml 文件中:


junit
junit
4.12

步骤 2:增加工具类 MyBatisUtil

package org.mybatis.example.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**

  • MyBatis工具类
  • @author 大强
  • */
    public class MyBatisUtil {

    public static SqlSessionFactory sqlSessionFactory;

    static {
    try {
    // 从类路径下加载资源文件mybatis-config.xml
    String resource = "org/mybatis/example/mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    // 由 SqlSessionFactoryBuilder创建SqlSessionFactory
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**

    • 由 SqlSessionFactory创建SqlSession
    • @return
      */
      public static SqlSession getSqlSession() {
      return sqlSessionFactory.openSession();
      }

    /**

    • 关闭SqlSession
    • @param sqlSession
      */
      public static void closeSqlSession(SqlSession sqlSession) {
      if (sqlSession != null) {
      sqlSession.close();
      }
      }

}
步骤 3:增加测试类 AuthorMapperTest

package org.mybatis.example.mapper;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.mybatis.example.domain.Author;
import org.mybatis.example.util.MyBatisUtil;

/**

  • 博客测试类
  • @author 大强
  • */
    public class AuthorMapperTest {

    Logger logger = Logger.getLogger(AuthorMapperTest.class);

    // 注意:选中方法名,单击鼠标右键,选择 Run as -> JUnit Test,就能执行单个方法。

    /**

    • 查询Author测试方法br/>*/
      @Test
      public void selectAuthorTest() {
      logger.info("查询作者开始...");
      SqlSession session = MyBatisUtil.getSqlSession();
      AuthorMapper mapper = session.getMapper(AuthorMapper.class);
      Author author = mapper.selectAuthor(301);
      if (author != null) {
      logger.info(author.getId());
      logger.info(author.getUsername());
      }
      }

    /**

    • 新增Author测试方法br/>*/
      @Test
      public void insertAuthorTest() {
      SqlSession session = MyBatisUtil.getSqlSession();
      logger.info("新增作者开始...");
      AuthorMapper mapper = session.getMapper(AuthorMapper.class);
      Author insertAuthor = new Author();insertAuthor.setPassword("12345678");
      insertAuthor.setEmail("[email protected]");
      br/>insertAuthor.setId(304);
      insertAuthor.setUsername("克林顿insert");
      insertAuthor.setPassword("12345678");
      insertAuthor.setEmail("[email protected]");
      insertAuthor.setBio("MyBatis团队成员");
      insertAuthor.setFavouriteSection("打球");
      mapper.insertAuthor(insertAuthor);
      session.commit();
      }

    /**

    • 修改Author测试方法br/>*/
      @Test
      public void updateAuthorTest() {
      SqlSession session = MyBatisUtil.getSqlSession();
      AuthorMapper mapper = session.getMapper(AuthorMapper.class);
      logger.info("修改作者开始...");
      Author updateAuthor = new Author();updateAuthor.setPassword("12345678");
      updateAuthor.setEmail("[email protected]");
      br/>updateAuthor.setId(304);
      updateAuthor.setUsername("布兰登update");
      updateAuthor.setPassword("12345678");
      updateAuthor.setEmail("[email protected]");
      updateAuthor.setBio("MyBatis团队成员");
      updateAuthor.setFavouriteSection("跑步");
      mapper.updateAuthor(updateAuthor);
      session.commit();
      logger.info("修改作者结束");
      session.commit();
      }

    /**

    • 删除Author测试方法br/>*/
      @Test
      public void deleteAuthorTest() {
      SqlSession session = MyBatisUtil.getSqlSession();
      AuthorMapper mapper = session.getMapper(AuthorMapper.class);
      mapper.deleteAuthor(304);
      session.commit();
      }
      }