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();
    }
}
相关推荐
起个名特麻烦1 分钟前
SpringBoot全局配置LocalDate/LocalTime/LocalDateTime的序列化和反序列化
java·spring boot·后端
高斯林.神犇6 分钟前
四、依赖注入.spring
java·后端·spring
hero.fei10 分钟前
在springboot中使用Resilience4j
java·spring boot·后端
沐苏瑶10 分钟前
Java算法之排序
java·算法·排序算法
java1234_小锋16 分钟前
Java高频面试题:Redis是单线程还是多线程?
java·redis·面试
工具罗某人16 分钟前
docker实现redis-cluster模式集群部署
java·redis·docker
用户2986985301417 分钟前
后端一次搞定 HTML 转 PDF?不装浏览器也能干的简易方案
java·后端
摇滚侠31 分钟前
从 Vibe Coding 到 Spec Coding:研发范式演进与高质量交付
java·人工智能·ai编程
兰.lan37 分钟前
【黑马ai测试】黑马头条登录功能测试-发布功能测试-其他功能模块设计
软件测试·人工智能·笔记·python·功能测试·ai·单元测试
希望永不加班1 小时前
SpringBoot 定时任务:@Scheduled 基础与动态定时
java·spring boot·后端·spring