Art*_*ich 2 java junit unit-testing
在我的测试类中,我使用了 junit 的临时文件夹。然后我在这个文件夹中创建一个新的 pdf 文件,写入这个文件并做出我的断言。我的单元测试看起来像:
@Test
public void testGeneratePdfIntegration1() throws Exception {
InputStream isMxml = SuperBarcodeProcTest.class.getResourceAsStream(RES_MXML_JOB_1);
InputStream isThumb = SuperBarcodeProcTest.class.getResourceAsStream(RES_PNG_JOB_1);
InputStream isPpf = SuperBarcodeProcTest.class.getResourceAsStream(RES_PPF_JOB_1);
Path destination = tempFolder.newFile("target1.pdf").toPath();
superBarcodeProc = new SuperBarcodeProc(isThumb, isMxml, isPpf, destination.toString());
superBarcodeProc.setDescription("Bogen: 18163407_01_B04_ST_135gl_1000_1-1");
superBarcodeProc.setBarcode("18163407_01");
superBarcodeProc.generatePdf();
assertTrue(Files.exists(destination));
assertTrue(Files.size(destination) > 1024);
}
Run Code Online (Sandbox Code Playgroud)
测试结束后,临时文件夹被删除。问题是我有多个单元测试,它们在同一个临时文件夹中生成具有不同设置的 pdf 文件,就像我提供的代码中的测试一样,当我在类中运行所有测试时,只有第一个成功。我的猜测是,在第一个测试结束后,临时文件夹消失了,其他测试失败,并显示 IOException 表示系统找不到给定的路径。问题是如何在不删除文件夹的情况下为多个单元测试使用同一个文件夹,或者这是不可能的,我必须为每个测试用例创建一个临时文件夹?
我假设您尝试使用org.junit.rules.TemporaryFolder. 如果要为所有非静态测试方法保留相同的测试文件夹,可以使用:
@ClassRule
public static TemporaryFolder outputFolder = new TemporaryFolder();
Run Code Online (Sandbox Code Playgroud)
仅供参考 - 以下语法将在每个 @Test 方法之前创建一个新文件夹,并在每个方法执行后进行清理。
@Rule
public TemporaryFolder outputFolder = new TemporaryFolder();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1990 次 |
| 最近记录: |