当前位置: 首页 > news >正文

建设门户网站的申请顺德区网站设计

建设门户网站的申请,顺德区网站设计,医疗网站建设哪家好,郑州网站建设学校junit 测试目录JUnit 4 TemporaryFolder Rule允许开发人员使用临时目录创建测试。 使用JUnit 5时#xff0c;不支持Rule因此测试文件和目录需要一点点额外的工作。 幸运的是#xff0c;有了JUnit 5.4#xff0c;有一个新的内置扩展可以处理测试中的临时目录。 而且它非常易于… 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 或File或Path类型的测试方法。 完成此操作后将创建临时目录。 一旦测试方法或类执行完毕将删除在测试执行过程中创建的目录及其内容。 要测试的代码 在这个简单的示例中我们将测试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您可以轻松地在测试中使用临时目录。 这里没有魔术您可以注释Path或File对象并根据需要进行注入。 其余的工作由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.htmljunit 测试目录
http://www.huolong8.cn/news/64592/

相关文章:

  • 如何做网站分析设置wordpress首页显示文章摘要
  • 免费建一个自己的网站y3微信管理系统
  • 在线做logo印章网站怎么看网站蜘蛛
  • 在线流程图网站怎么做商城网站开发报
  • 公司做网站建设做网站用什么浏览器最好
  • 大连三丰建设集团公司网站河北工商注册网入口
  • 正常做网站多少钱做网站的公司 杭州
  • 青岛城乡住房建设厅网站建设银行网站修改手机号
  • 90设计包图网在线工具seo
  • 推荐昆明做网站建设邯郸网站制作外包
  • 西安搬家公司电话号码邢台视频优化排名
  • 如何做网站性能优化九游下载安装载
  • 中铁建设集团门户网站登录学程序员需要什么基础
  • 中国手机网站建设公司上海住房和城乡建设厅网站
  • 用ps个人网站怎么做网站建设过程中的通用原则
  • 网站图片的暗纹是怎么做的西安app制作开发公司
  • 软装设计网站大全网站备案到期
  • 如何在网站上做推广网站产品网页设计模板
  • dedecms医院网站网页设计模板html代码软件
  • 网站建设公司怎么投诉电商平台的发展
  • 怎么做自己的网站移动网站开发认证考试
  • 外贸网站推广方案wordpress 店铺推荐
  • 建设一个小网站需要多少钱网站优化是做什么的
  • 广东深圳网站建设微信商城开发快递服务平台
  • 地方门户网站策划书html如何做自己的网站
  • 邯郸贴吧网站个人域名做企业网站
  • 关键词爱站网关键词挖掘工具外贸平台网站
  • 提供网站建设工具做网站排版
  • 网站建设推广方式网站建设思维
  • 手工做衣服网站建模师培训机构有哪些