day26-单元测试

1. 单元测试Junit

1.1 什么是单元测试?(掌握)

1.2 Junit的特点?(掌握)

1.3 基本用法:(掌握)


实际开发中单元测试的使用方式(掌握)


java 复制代码
public class TestDemo {
    public int addMethod(int a,int b){
        return a+b;
    }
}
java 复制代码
public class Main {
    @Test
    public void method(){
        TestDemo testDemo = new TestDemo();
        int result = testDemo.addMethod(3, 4);

        Assert.assertEquals("add方法错了",result,7);
    }
}



java 复制代码
public class Main {


    @Before
    public void beforeMethod() throws IOException {
        //先备份
        File src = new File("a.txt");
        File dest = new File("b.txt");

        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(dest);

        int b;
        while ((b=fis.read())!=-1){
            fos.write(b);
        }
        fos.close();
        fis.close();
    }

    @Test
    public void testMethod(){
        File file = new File("a.txt");
        //删除文件
        boolean result = file.delete();
        //文件是否存在
        boolean exists = file.exists();

        //只有同时满足了,才表示delete方法正确
        Assert.assertEquals("delete方法错了",result,true);
        Assert.assertEquals("delete方法错了",exists,false);
    }

    @After
    public void afterMethod() throws IOException {
        //还原数据
        File dest = new File("a.txt");
        File src = new File("b.txt");

        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(dest);

        int b;
        while ((b=fis.read())!=-1){
            fos.write(b);
        }
        fos.close();
        fis.close();

        //删除备份数据
        src.delete();
    }
}
相关推荐
步行cgn6 小时前
Spring c 命名空间注入详解
java·后端·spring
明月_清风6 小时前
Maven 到底是什么?一篇文章搞懂 Java 项目构建与依赖管理
java·后端·maven
aramae6 小时前
MySQL复合查询(8)
java·c语言·开发语言·后端·算法
Rain的Java大神之路7 小时前
如何快速上传10G文件
java·spring boot·redis·后端·mysql·spring cloud·面试
Wang's Blog8 小时前
Java 接入Redis: 通用命令与键管理
java·服务器·redis
Wang's Blog8 小时前
Java 接入Redis: 列表集合与有序集合操作命令
java·服务器·redis
Ivanqhz8 小时前
SVD++算法
java·服务器·网络·深度学习·神经网络
qq_2518364579 小时前
springboot vue3 开发实现 拼豆管理系统
java·开发语言·ai编程
caoerzhong9 小时前
JeeWMS 开源仓库管理系统全景解读:一套 Java WMS 如何把 WMS/OMS/BMS/TMS 装进同一个系统
java·开源
曹牧9 小时前
Java集合框架:List、ArrayList、Map 和 HashMap
java