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();
    }
}
相关推荐
cat三三5 分钟前
java之异常
java·开发语言
浙江第二深情10 分钟前
前端性能优化终极指南
java·maven
养乐多072226 分钟前
【Java】IO流
java
俊男无期26 分钟前
超效率工作法
java·前端·数据库
中国胖子风清扬30 分钟前
SpringAI和 Langchain4j等 AI 框架之间的差异和开发经验
java·数据库·人工智能·spring boot·spring cloud·ai·langchain
月明长歌42 分钟前
【码道初阶】牛客TSINGK110:二叉树遍历(较难)如何根据“扩展先序遍历”构建二叉树?
java·数据结构·算法
用户2190326527351 小时前
Spring Boot + Redis 注解极简教程:5分钟搞定CRUD操作
java·后端
Alice1 小时前
linux scripts
java·linux·服务器
Filotimo_1 小时前
Spring Data JPA 方法名查询特性的使用
java·开发语言·windows
代码栈上的思考1 小时前
MyBatis:注解方式实现数据库 CRUD 全操作
java·开发语言·mybatis