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();
    }
}
相关推荐
dadaobusi11 分钟前
PCIe的ATS和PRS
java·网络·数据库
南境十里·墨染春水18 分钟前
线程池学习(二)线程池理解
java·jvm·学习
ZGi.ai20 分钟前
私有化大模型接入企业系统:SSO+权限+API网关完整方案
java·开发语言·大模型·私有化部署·sso·企业架构
Han_han91921 分钟前
集合进阶(Map集合):
java
吴声子夜歌23 分钟前
Java——文件和目录操作
java·文件·目录
吴声子夜歌27 分钟前
Java——随机
java·random·随机
aaaak_30 分钟前
PDD 直播间 评论 , wss hex Protobuf 解析流程分析学习
java·前端·学习
小雅痞34 分钟前
[Java][Leetcode simple] 205. 同构字符串
java·算法·leetcode
多加点辣也没关系1 小时前
设计模式-策略模式
java·设计模式·策略模式
2601_953660371 小时前
Java Map集合详解与实战
java·开发语言·python