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();
    }
}
相关推荐
Java中文社群1 小时前
重要:Java25正式发布(长期支持版)!
java·后端·面试
每天进步一点_JL2 小时前
JVM 类加载:双亲委派机制
java·后端
用户298698530142 小时前
Java HTML 转 Word 完整指南
java·后端
渣哥3 小时前
原来公平锁和非公平锁差别这么大
java
渣哥3 小时前
99% 的人没搞懂:Semaphore 到底是干啥的?
java
J2K3 小时前
JDK都25了,你还没用过ZGC?那真得补补课了
java·jvm·后端
kfyty7253 小时前
不依赖第三方,不销毁重建,loveqq 框架如何原生实现动态线程池?
java·架构
isysc14 小时前
面了一个校招生,竟然说我是老古董
java·后端·面试
道可到8 小时前
Java 反射现代实践速查表(JDK 11+/17+)
java
道可到8 小时前
Java 反射现代实践指南(JDK 11+ / 17+ 适用)
java