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();
    }
}
相关推荐
老神在在0012 分钟前
测试方法与使用场景
单元测试·测试
人道领域6 分钟前
【黑马点评日记】社交平台用户关注功能全解析Feed流相关操作
java·开发语言·数据库·redis·python
shehuiyuelaiyuehao36 分钟前
算法18,二分查找
java·开发语言·算法
weixin_4467291641 分钟前
java网络通讯
java·开发语言
NE_STOP41 分钟前
Redis--集群搭建与主从复制原理
java
好奇的菜鸟43 分钟前
Java开发常用中间件,Docker安装。
java·docker·中间件
许泽宇的技术分享1 小时前
别再把 AI Agent 当“会聊天的脚本”:Hermes Agent 源码级拆解(架构、框架、实战、趋势,一文吃透)
java·linux·网络
程序员三明治1 小时前
【AI】Prompt 工程入门:从五要素框架到 RAG 生产级 Prompt 模板与 Java 实战
java·人工智能·后端·大模型·llm·prompt·agent
旷世奇才李先生1 小时前
React 18\+Next\.js 14实战:服务端渲染与跨端开发全指南
java·人工智能·python
跨境技工小黎1 小时前
亚马逊数据抓取怎么做?2026最新实战教程
java·大数据·数据库