关闭 x
IT技术网
    技 采 号
    ITJS.cn - 技术改变世界
    • 实用工具
    • 菜鸟教程
    IT采购网 中国存储网 科技号 CIO智库

    IT技术网

    IT采购网
    • 首页
    • 行业资讯
    • 系统运维
      • 操作系统
        • Windows
        • Linux
        • Mac OS
      • 数据库
        • MySQL
        • Oracle
        • SQL Server
      • 网站建设
    • 人工智能
    • 半导体芯片
    • 笔记本电脑
    • 智能手机
    • 智能汽车
    • 编程语言
    IT技术网 - ITJS.CN
    首页 » JAVA »在JUnit中使用@Rule测试文件和目录

    在JUnit中使用@Rule测试文件和目录

    2015-05-07 00:00:00 出处:偶my耶的博客
    分享

    伴随JUnit中 TemporaryFolder @Rule 的出现,测试文件和目录变得简单了。

    在 JUnit 中,规则(@Rule)可作为构造测试用具(fixture)时初始化方法和清理方法的替代和补充(在 JUnit 中,这2种方法分别通过以下注解标注:org.junit.Before、org.junit.After、org.junit.BeforeClass 和 org.junit.AfterClass) 。而且规则的功能更加强大并且也更易于在项目和类之间共享。

    译者注:测试用具是指作为测试运行基准的一组对象所呈现的一个稳定状态。其目的是确保测试是运行在一个众所周知的、稳定的环境中的,以实现测试的可重复执行。准备输入数据、生成模拟对象(Mock)、将特定的数据加载到数据库、复制一组特定的文件等,这些都属于构造测试用具。

    待测试的代码

    public void writeTo(String path, String content) throws IOException {
        Path target = Paths.get(path);
        if (Files.exists(target)) {
            throw new IOException("file already exists");
        }
        Files.copy(new ByteArrayInputStream(content.getBytes("UTF8")), target);
    }

    测试类

    public class FileWriterTest {
    
        private FileWriter fileWriter = new FileWriter();
    
        @Rule
        public TemporaryFolder temporaryFolder = new TemporaryFolder();
    
        @Rule
        public ExpectedException thrown = ExpectedException.none();
    
        @Test
        public void throwsErrorWhenTargetFileExists() throws IOException {
            // arrange
            File output = temporaryFolder.newFile("output.txt");
    
            thrown.expect(IOException.class);
            thrown.expectMessage("file already exists");
    
            // act
            fileWriter.writeTo(output.getPath(), "test");
        }
    
        @Test
        public void writesContentToFile() throws IOException {
            // arrange
            File output = temporaryFolder.newFolder("reports")
                    .toPath()
                    .resolve("output.txt")
                    .toFile();
    
            // act
            fileWriter.writeTo(output.getPath(), "test");
    
            // assert
            assertThat(output)
                    .hasContent("test")
                    .hasExtension("txt")
                    .hasParent(resolvePath("reports"));
        }
    
        private String resolvePath(String folder) {
            return temporaryFolder
                    .getRoot().toPath()
                    .resolve(folder)
                    .toString();
        }
    }

    译者注:第35行的 assertThat() 是类 org.assertj.core.api.Assertions 中的静态方法。

    TemporaryFolder 提供了2个方法 newFile 和 newFolder,分别用于管理文件和目录。这2个方法都可以返回所需要的对象。返回的文件或目录都是由 setup 方法创建的并被存放在临时目录中。要想获取临时目录自身的路径,可以使用 TemporaryFolder 的 getRoot 方法。

    无论测试成功与否,任何在测试过程中添加到临时目录的文件或目录都会在测试结束时删除。

    本示例可以从我在 GitHub 上的项目 unit-testing-demo 中找到,除此之外该项目中还有很多其他示例可供诸位参考。

    上一篇返回首页 下一篇

    声明: 此文观点不代表本站立场;转载务必保留本文链接;版权疑问请联系我们。

    别人在看

    小米路由器买哪款?Miwifi热门路由器型号对比分析

    DESTOON标签(tag)调用手册说明(最新版)

    Destoon 9.0全站伪静态规则设置清单(Apache版)

    Destoon 9.0全站伪静态规则设置清单(Nginx版)

    Destoon 8.0全站伪静态规则设置清单(Apache版)

    Destoon 8.0全站伪静态规则设置清单(Nginx版)

    Destoon会员公司地址伪静态com/目录如何修改?两步轻松搞定,适合Nginx和Apache

    Python 并行处理列表的常见方法及其优缺点分析

    正版 Windows 11产品密钥怎么查找/查看?

    还有3个月,微软将停止 Windows 10 的更新

    IT头条

    StorONE 的高效平台将 Storage Guardian 数据中心占用空间减少 80%

    11:03

    年赚千亿的印度能源巨头Nayara 云服务瘫痪,被微软卡了一下脖子

    12:54

    国产6nm GPU新突破!砺算科技官宣:自研TrueGPU架构7月26日发布

    01:57

    公安部:我国在售汽车搭载的“智驾”系统都不具备“自动驾驶”功能

    02:03

    液冷服务器概念股走强,博汇、润泽等液冷概念股票大涨

    01:17

    技术热点

    最常用的 Eclipse 快捷键整理

    多表多查询条件对SQL Server查询性能的优化

    浅谈如何优化SQL Server服务器

    HTTP 协议中使用 Referer Meta 标签控制 referer

    好用的mysql备份工具

    Android开发中的MVP架构详解

      友情链接:
    • IT采购网
    • 科技号
    • 中国存储网
    • 存储网
    • 半导体联盟
    • 医疗软件网
    • 软件中国
    • ITbrand
    • 采购中国
    • CIO智库
    • 考研题库
    • 法务网
    • AI工具网
    • 电子芯片网
    • 安全库
    • 隐私保护
    • 版权申明
    • 联系我们
    IT技术网 版权所有 © 2020-2025,京ICP备14047533号-20,Power by OK设计网

    在上方输入关键词后,回车键 开始搜索。Esc键 取消该搜索窗口。