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();
    }
}
相关推荐
weixin_307779131 分钟前
Jenkins Branch API插件详解:多分支项目管理的核心引擎
java·运维·开发语言·架构·jenkins
milanyangbo6 分钟前
从硬盘I/O到网络传输:Kafka与RocketMQ读写模型及零拷贝技术深度对比
java·网络·分布式·架构·kafka·rocketmq
小股虫6 分钟前
消息中间件关键技术、设计原理与实现架构总纲
java·开发语言·架构
风萧萧19998 分钟前
Java:PPT转图片
java·python·powerpoint
洲星河ZXH9 分钟前
Java,日期时间API
java·开发语言·python
老华带你飞15 分钟前
动物救助|流浪狗救助|基于Springboot+vue的流浪狗救助平台设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·流浪动物救助平台
Hernon18 分钟前
微服务架构设计:从零打造互联网车贷系统 —— 业务背景与架构蓝图
java·微服务·架构·微服务架构设计
9523620 分钟前
二叉平衡树
java·数据结构·学习·算法
Vic1010122 分钟前
Java正则表达式性能优化指南:编译开销、类加载与线程安全深度解析
java·性能优化·正则表达式
小二·29 分钟前
Spring框架入门:代理模式详解
java·spring·代理模式