junit 测试目录

JUnit 4 TemporaryFolder @Rule允许开发人员使用临时目录创建测试。 使用JUnit 5时,不支持@Rule因此测试文件和目录需要一点点额外的工作。 幸运的是,有了JUnit 5.4,有一个新的内置扩展可以处理测试中的临时目录。 而且它非常易于使用。

您还在使用JUnit 4吗? 请参阅我以前的有关使用TemporaryFolder @Rule在JUnit 4中测试文件和目录的文章。

@TempDir

可以使用@org.junit.jupiter.api.io.TempDir注释来注释类字段或生命周期中的参数(例如@BeforeEach )或FilePath类型的测试方法。 完成此操作后,将创建临时目录。 一旦测试方法或类执行完毕,将删除在测试执行过程中创建的目录及其内容。

要测试的代码

在这个简单的示例中,我们将测试FileWriter类,该类具有将文本内容写入新文件的单个方法:

 public class FileWriter { 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(StandardCharsets.UTF_8)), target); }  } 

@TemDir作为测试方法参数

在此示例中,我们将使用@TempDir注释对测试参数进行注释:

 import org.junit.jupiter.api.io.TempDir;  @Test  void writesContentToFile( @TempDir Path tempDir) throws IOException { // arrange Path output = tempDir .resolve( "output.txt" ); // act fileWriter.writeTo(output.toString(), "test" ); // assert assertAll( () -> assertTrue(Files.exists(output)), () -> assertLinesMatch(List.of( "test" ), Files.readAllLines(output)) );  } 

@TempDir作为实例字段

 import org.junit.jupiter.api.io.TempDir;  class FileWriterTest { private FileWriter fileWriter = new FileWriter(); @TempDir Path tempDir; @BeforeEach void beforeEach() { assertTrue(Files.isDirectory( this .tempDir)); } @RepeatedTest ( 3 ) void throwsErrorWhenTargetFileExists() throws IOException { // arrange Path output = Files.createFile( tempDir.resolve( "output.txt" ) ); // act & assert IOException expectedException = assertThrows(IOException. class , () -> fileWriter.writeTo(output.toString(), "test" )); assertEquals( "file already exists" , expectedException.getMessage()); }  } 

根据上面的示例,我们可以看到每次重复测试都使用一个新的临时目录(根据标准测试类生命周期),因此该方法的ranging部分执行无误。

共享的临时目录

如果需要在测试方法之间共享一个临时目录,我们可以创建一个静态字段并重复使用该临时目录,如以下示例所示:

 import org.junit.jupiter.api.io.TempDir;  class FileWriterTest { private FileWriter fileWriter = new FileWriter(); @TempDir static Path tempDir; @BeforeAll static void setUp() { assertTrue(Files.isDirectory(tempDir)); } @RepeatedTest ( 3 ) void throwsErrorWhenTargetFileExists(RepetitionInfo repetitionInfo) throws IOException { // arrange Path output = Files.createFile( tempDir.resolve(repetitionInfo.getCurrentRepetition() + "_output.txt" ) ); // act & assert IOException expectedException = assertThrows(IOException. class , () -> fileWriter.writeTo(output.toString(), "test" )); assertEquals( "file already exists" , expectedException.getMessage()); }  } 

请注意,测试方法的FileAlreadyExistsException会在每次执行时(使用当前的重复计数器)创建唯一的文件名,否则会抛出FileAlreadyExistsException

摘要

使用@TempDir您可以轻松地在测试中使用临时目录。 这里没有魔术:您可以注释PathFile对象并根据需要进行注入。 其余的工作由JUnit替您完成。

在我的GitHub存储库中找到示例: https : //github.com/kolorobot/junit5-samples/tree/master/junit5-built-in-extensions

翻译自: https://www.javacodegeeks.com/2019/03/temporary-directories-junit-5-tests.html

junit 测试目录

junit 测试目录_JUnit 5测试中的临时目录相关推荐

  1. JUnit 5测试中的临时目录

    JUnit 4 TemporaryFolder @Rule允许开发人员使用临时目录创建测试. 使用JUnit 5时,不支持@Rule因此测试文件和目录需要一些额外的工作. 幸运的是,有了JUnit 5 ...

  2. linux nginx php 目录权限,Nginx环境中配置网站目录权限设置

    在Nginx与php环境下,务必要设置好Nginx目录权限,安全的目录权限设置,将是网站安全的一道屏障,有需要的朋友做个参考吧. 配置权限的原则是,在保证网站正常运行下,尽量给最低权限. nginx目 ...

  3. linux创建pc目录,在linux汇编语言中创建一个目录

    的一种方法,是使用GCC来翻译以下C代码: #include #include int main() { if (mkdir("testdir", 0777) != 0) { re ...

  4. 内容 超链接_excel中如何设置目录与返回目录超链接?这两种方法很简单

    在excel操作中,我们可以按下Ctrl+K快捷键使用超链接功能,包括链接到网页.文件.工作表.单元格等.但是如果同一个工作簿中有很多工作表,那么该如何设置目录,并且通过超链接的功能实现目录与工作表之 ...

  5. 计算机二级目录设置,word2设置标题格式,生成目录,奇偶页设置等等,适用考计算机二级办公软件,也适用于毕业论文格式设置...

    word2设置标题格式,生成目录,奇偶页设置等等,适用考计算机二级办公软件,也适用于毕业论文格式设置 这是第2部分word资料 , 跟之前的第一部份word资料合为一份 第二题 2.在正文前按序插入节 ...

  6. linux下mysql数据库目录迁移_mysql实现linux下数据库目录迁移

    1.查看mysql安装目录 从目录etc/my.cnf中查看安装目录 2.进入mysql目录,停止mysql服务cd usr/local/mysql service mysql stop (相关文章教 ...

  7. junit不启用事务_JUnit禁用启用测试

    junit不启用事务 JUnit 5 Jupiter API provides various ways to disable or enable a test. There are various ...

  8. maven在idea中使用junit时,运行要测试的类,提示class not found。

    maven在idea中使用junit时,运行要测试的类,提示class not found.需要把.idea配置文件删了,然后重启,之后就可以了

  9. Junit单元测试不支持多线程测试问题全解析

    一.背景 今天@段段提出了一个很好的问题,她发现单元测试时如果开多个线程,主线程运行结束就结束了,并不会等待子线程结束. 如果用main方法就没问题,技术群里展开了激烈的讨论. 本文将"复现 ...

最新文章

  1. JS Array 中 shift 和 pop 的妙用
  2. java面向对象特征及阐述,Java面向对象四个特征
  3. matlab破损皮革定位,皮革下料
  4. android studio 导入第三方库的记录
  5. leetcode 290. 单词规律(Java版)
  6. python连接阿里云数据库_python连接阿里云数据库
  7. spark.mllib:回归算法
  8. class_create和class_device_create
  9. 那些你不知道的程序员的多重身份
  10. springboot中使用servlet通过配置类
  11. Linux内核多线程(二)
  12. 基于OptiSystem的高速远距离光纤通信系统研究
  13. 油猴脚本(Tampermonkey)的简介
  14. log4j的日志级别以及配置
  15. 苹果电脑Chrome浏览器截网页长图
  16. 广告点击率预估是怎么回事?
  17. Ajax学习日志(五)—— 如何传递json格式请求参数
  18. 三角脉冲信号的表达式_三角形脉冲信号怎么用斜变信号表示?为什么当t=τ时,图上的线是连下来的...
  19. 零基础自学Python好难?学起来很吃力,想放弃?看看别人是怎样学习的
  20. RL(Chapter 3): Finite Markov Decision Processes (有限马尔可夫决策过程)

热门文章

  1. C. The Sports Festival
  2. Maximize The Beautiful Value
  3. jzoj3301-[集训队互测2013]家族【并查集,暴力】
  4. hdu3666-THE MATRIX PROBLEM【差分约束,自然对数】
  5. P3830-[SHOI2012]随机树【数学期望,dp】
  6. 【DP】合唱队形(jzoj 1122)
  7. 字符串(AC自动机(fail tree))
  8. 4、mysql数据库的权限管理
  9. 介绍Java中的内存泄漏
  10. C++描述杭电OJ 2021.发工资 ||