使用相同的junit临时文件夹进行多个单元测试 [英] Use same junit temporary folder for multiple unit tests

查看:67
本文介绍了使用相同的junit临时文件夹进行多个单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试类中,我使用junit的temp文件夹.然后,我在此文件夹中创建一个新的pdf文件,写入该文件并进行声明.我的单元测试如下:

In my test class I use junit's temp folder. Then I create a new pdf file in this folder, write to this file and make my assertions. My unit test looks like:

@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);
}

测试结束后,将删除temp文件夹.问题是我有多个单元测试,它们在同一个temp文件夹中生成具有不同设置的pdf文件,就像我提供的代码中的测试一样,当我在该类中运行所有测试时,只有第一个成功.我的猜测是,在第一个测试结束后,临时文件夹消失了,而其他测试失败,并出现IOException,提示系统找不到给定的路径.问题是如何在不删除文件夹的情况下如何将同一文件夹用于多个单元测试,或者这是不可能的,我必须为每个测试用例创建一个临时文件夹?

After the test ends, the temp folder is being deleted. The problem is that I have multiple unit tests which generate pdf file with different settings in the same temp folder like the test in the code I've provided and when I run all tests in the class only the first one successes. My guess is that after the first test ends the temp folder is gone and the other tests fail with the IOException saying that the system cannot find the given path. The question is how can I use the same folder for multiple unit tests without the folder being deleted or is this impossible and I have to create a temp folder for each test case?

推荐答案

我找到了解决方案.我宣布我的临时文件夹为静态文件夹,这导致了上述问题.

I found the solution. I declared my temp folder to be static which caused the above problem.

这篇关于使用相同的junit临时文件夹进行多个单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆