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();
    }
}
相关推荐
Android小码家10 分钟前
Vehicle 实战 自定义属性+测试demo
java·c++·framework·vhal·vehicle
范同学~10 分钟前
固定表头+第一列+最后一列
java·服务器·前端
随风爱学习27 分钟前
JAVA学习之异常
java
m0_6399069430 分钟前
HashMap:哈希扰动、扩容源码、红黑树转换、并发问题
java·算法·哈希算法
Roll圈圈37 分钟前
长链接太丑影响转化率?这个短链工具让你的推广链接更专业
java
神魔皆以血嗜43 分钟前
Java 存储金额
java·后端
SL_staff2 小时前
周报越厚问题越拖?用结构化执行机制让周报回归决策价值
java·spring cloud·开源
梅梅绵绵冰2 小时前
JVM虚拟机
java·开发语言·jvm
golang学习记2 小时前
Python vs Go:2026年到底该怎么选
java·vscode·python·golang·pycharm·goland
Shaoshing2 小时前
消息顺序
java·开发语言