Junit测试介绍

Junit测试介绍

Junit的使用步骤

添加Junit依赖

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

生成测试类
找到要生成测试类的当前位置
在这里插入图片描述
选择要生成测试的方法
生成测试类

public class StudentMapper213Test {
@Test
    public void selectStudentById() {
        String resource = "mybatis-config.xml";
        InputStream stream = null;
        try {
            stream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        //获取会话
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过接口获取对象实例
        StudentMapper213 studentMapper213 = sqlSession.getMapper(StudentMapper213.class);

        //测试接口
            Student213 student213 = studentMapper213.selectStudentById(1);
            System.out.println(student213);
}
  @Test
    public void deleteStudentById() {
    }
}

Junit介绍

Junit是用于编写和运行可重复的自动化测试的开源框架
适用于测试整个对象
对象的一部分,交互中的一个方法或者是一些方法
对象之间的交互

常用的注解:
@Before
当写测试方法是,会防线一些方法之前会执行创建相同的对象,可以将其放入before中操作使用before注解的方法一般是public void 方法名 ,就会在@test方法之前被执行

@Test
tese注解的public void方法将被作为测试用例
Junit每次都会创建一个新的测试实例,然后调用@test注解的方法

    @Test
    public void deleteStudentById() {
    }

@After
如果分配了额外的资源,在测试完成之后需要关闭资源,
使用after注解给定一个public void 方法在test注解的方法执行完成之后在执行

SqlSessionFactory sqlSessionFactory = null;
@Before
    public void Before(){
        String resource = "mybatis-config.xml";
        InputStream stream = null;
        try {
            stream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        System.out.println("before方法执行完成");
    }
    @Test
    public void selectStudentById() {
        System.out.println("测试select方法");
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过接口获取对象实例
        StudentMapper213 studentMapper213 = sqlSession.getMapper(StudentMapper213.class);
	//测试接口
            Student213 student213 = studentMapper213.selectStudentById(1);
            System.out.println(student213);
            }
            @Test
            public void deleteStudentById() {
            //获取会话
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过接口获取对象实例
        StudentMapper213 studentMapper213 = sqlSession.getMapper(StudentMapper213.class);
        studentMapper213.deleteStudentById(19);
        sqlSession.commit();
    }
    @After
    public void  after() {
        System.out.println("after方法执行结束");
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值