junit 测试目录_JUnit 5测试中的临时目录
junit 測(cè)試目錄
JUnit 4 TemporaryFolder @Rule允許開(kāi)發(fā)人員使用臨時(shí)目錄創(chuàng)建測(cè)試。 使用JUnit 5時(shí),不支持@Rule因此測(cè)試文件和目錄需要一點(diǎn)點(diǎn)額外的工作。 幸運(yùn)的是,有了JUnit 5.4,有一個(gè)新的內(nèi)置擴(kuò)展可以處理測(cè)試中的臨時(shí)目錄。 而且它非常易于使用。
您還在使用JUnit 4嗎? 請(qǐng)參閱我以前的有關(guān)使用TemporaryFolder @Rule在JUnit 4中測(cè)試文件和目錄的文章。
@TempDir
可以使用@org.junit.jupiter.api.io.TempDir注釋來(lái)注釋類(lèi)字段或生命周期中的參數(shù)(例如@BeforeEach )或File或Path類(lèi)型的測(cè)試方法。 完成此操作后,將創(chuàng)建臨時(shí)目錄。 一旦測(cè)試方法或類(lèi)執(zhí)行完畢,將刪除在測(cè)試執(zhí)行過(guò)程中創(chuàng)建的目錄及其內(nèi)容。
要測(cè)試的代碼
在這個(gè)簡(jiǎn)單的示例中,我們將測(cè)試FileWriter類(lèi),該類(lèi)具有將文本內(nèi)容寫(xiě)入新文件的單個(gè)方法:
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作為測(cè)試方法參數(shù)
在此示例中,我們將使用@TempDir注釋對(duì)測(cè)試參數(shù)進(jìn)行注釋:
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作為實(shí)例字段
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()); } }根據(jù)上面的示例,我們可以看到每次重復(fù)測(cè)試都使用一個(gè)新的臨時(shí)目錄(根據(jù)標(biāo)準(zhǔn)測(cè)試類(lèi)生命周期),因此該方法的ranging部分執(zhí)行無(wú)誤。
共享的臨時(shí)目錄
如果需要在測(cè)試方法之間共享一個(gè)臨時(shí)目錄,我們可以創(chuàng)建一個(gè)靜態(tài)字段并重復(fù)使用該臨時(shí)目錄,如以下示例所示:
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()); } }請(qǐng)注意,測(cè)試方法的FileAlreadyExistsException會(huì)在每次執(zhí)行時(shí)(使用當(dāng)前的重復(fù)計(jì)數(shù)器)創(chuàng)建唯一的文件名,否則會(huì)拋出FileAlreadyExistsException 。
摘要
使用@TempDir您可以輕松地在測(cè)試中使用臨時(shí)目錄。 這里沒(méi)有魔術(shù):您可以注釋Path或File對(duì)象并根據(jù)需要進(jìn)行注入。 其余的工作由JUnit替您完成。
在我的GitHub存儲(chǔ)庫(kù)中找到示例: 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 測(cè)試目錄
總結(jié)
以上是生活随笔為你收集整理的junit 测试目录_JUnit 5测试中的临时目录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 3好看的电脑桌面(电脑壁纸)
- 下一篇: rxjava背压怎样使用_使用Micro