使用测试类Test程序

测试类

简单来说就是专门用来测试别的类的类,该类没有main方法。

为什么用测试类

假如写了个操作数据库增删改查的工具类,但是想测试一下代码功能是否正确。
如果不用测试类,我必须声明个main方法,在里面一个个调用每个方法,调用完insert方法之后将它注释,再调用下一个…以此类推。手动注释非常麻烦,假如后面我们需要扩展insert的功能,继续测试insert(),又要将其他方法注释掉,非常不方便。而且在该类中测试,不利于代码的整洁更不符合规范(Java中强调,一个类应该专注于做一件事,即做好她的本职工作。然而这个类主要用于CRDU操作,不应该同时用来测试)

public class CRUDDemo {
    private  Connection conn = DBUtil.getConnection();
    private PreparedStatement ps;
    private ResultSet rs;
    /*
     * 添加
     */
    public int insert(String sql,int id,String name,double salary,String gender) throws SQLException {
        ...
    }

    /*
     * 修改
     */
    public int update(String sql,String name,int id)throws SQLException {
        ...
    }

    /*
     * 删除
     */
    public int delete(String sql,int id) throws SQLException {
        ...
    }

    /*
     * 简单查询
     */
    public void select(String sql)throws SQLException {
        ...

    }

    public static void main(String[] args) throws SQLException {
        CRUDDemo cd = new CRUDDemo();
        // 测试增加
        String insertSql = "insert into employee values(?,?,?,?)";
        cd.insert(insertSql,3,"谢春花",12000.00,"女");
        // 测试修改(更新),假设更新姓名,更新其他字段类似
        String updateSql = "update employee set name=? where id =? ";
        cd.update(updateSql, "银临", 1);
        // 测试删除
        String deleteSql = "delete form employee where id = ?";
        cd.delete(deleteSql, 2);
        // 测试查询表中所有数据
        String selectSql = "select * from employee";
        cd.select(selectSql);
        // 测试带条件的查询(以按id查询为例,其他查询以此类推)
        String selectSql1 = "select * from employee where id = ?";
        cd.select(selectSql1,3);


    }
}

命名规则

测试类的类名以Test开头,后面紧跟着要测试的类名,假如,要测试的类是CRDUDemo,它的测试类名应该是TestCRDUDemo。

测试类应该单独放一个包里。

使用方法

  1. 新建一个包名为test,在包上右键-New-JUnit TestCase,如果右键没有就点击Other..如图:
    这里写图片描述
    2.步骤1的图点击next得到下图。name是测试类名,点击Browser浏览要测试的类。
    这里写图片描述
  2. 搜索要测试的类,点击选择之后ok。
    这里写图片描述
    4.点击下一步
    这里写图片描述
    5.选择要测试类中哪些方法,选好之后finish
    这里写图片描述
  3. 将方法中默认的fail(“Not yet implemented”);删除,写上我们的测试内容:
package test;

import java.sql.SQLException;

import org.junit.jupiter.api.Test;

import Beans.CRUDDemo;

class TestCRUDDemo {
    CRUDDemo cd = new CRUDDemo();
    @Test
    void testInsert() throws SQLException {
        // 测试增加
        String insertSql = "insert into employee values(?,?,?,?)";
        cd.insert(insertSql,3,"谢春花",12000.00,"女");
    }

    @Test
    void testUpdate() throws SQLException {
        // 测试修改(更新),假设更新姓名,更新其他字段类似
        String updateSql = "update employee set name=? where id =? ";
        cd.update(updateSql, "银临", 1);
    }

    @Test
    void testDelete() throws SQLException {
        // 测试删除
        String deleteSql = "delete form employee where id = ?";
        cd.delete(deleteSql, 2);
    }

    @Test
    void testSelectString() throws SQLException {
        // 测试查询表中所有数据
        String selectSql = "select * from employee";
        cd.select(selectSql);
    }

    @Test
    void testSelectStringInt() throws SQLException {
        // 测试带条件的查询(以按id查询为例,其他查询以此类推)
        String selectSql1 = "select * from employee where id = ?";
        cd.select(selectSql1,3);
    }

}

7.双击方法名,右键Run As.. JUnitTest,绿条代表测试通过,红色代表未通过。
这里写图片描述
8. 我故意将sql语句写错,它下面会提示错误。根据提示找出即可。
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_32106647/article/details/79146654
今日推荐