JUnit 规则临时文件夹

发布于 2024-08-30 15:12:06 字数 250 浏览 3 评论 0原文

我正在 JUnit 4.7 中使用 @Rule 注释创建一个 TemporaryFolder。我尝试在我的 @Before (设置)方法中使用 tempFolder.newFolder("someFolder") 创建一个新文件夹,该文件夹是临时文件夹的子文件夹测试。似乎临时文件夹在安装方法运行后被初始化,这意味着我无法在安装方法中使用临时文件夹。这是正确的(且可预测的)行为吗?

I'm creating a TemporaryFolder using the @Rule annotation in JUnit 4.7. I've tried to create a new folder that is a child of the temp folder using tempFolder.newFolder("someFolder") in the @Before (setup) method of my test. It seems as though the temporary folder gets initialized after the setup method runs, meaning I can't use the temporary folder in the setup method. Is this correct (and predictable) behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罗罗贝儿 2024-09-06 15:12:06

这是 Junit 4.7 中的问题。如果您升级较新的 Junit(例如 4.8.1),则当您输入 @Before 方法时,所有 @Rule 都将运行。相关的错误报告是这样的: https://github.com/junit-team/junit4 /问题/79

This is a problem in Junit 4.7. If you upgrade a newer Junit (for example 4.8.1) all @Rule will have been run when you enter the @Before method:s. A related bug report is this: https://github.com/junit-team/junit4/issues/79

情话墙 2024-09-06 15:12:06

这也有效。 编辑 如果在 @Before 方法中看起来需要调用 myfolder.create() 。这可能是不好的做法,因为 javadoc 说不要调用 TemporaryFolder.create()。 第二次编辑 看起来,如果您不想在 @Test 方法中使用它们,则必须调用方法来创建临时目录。另请确保关闭在临时目录中打开的所有文件,否则它们不会被自动删除。

<imports excluded>

public class MyTest {

  @Rule
  public TemporaryFolder myfolder = new TemporaryFolder();

  private File otherFolder;
  private File normalFolder;
  private File file;

  public void createDirs() throws Exception {
    File tempFolder = myfolder.newFolder("folder");
    File normalFolder = new File(tempFolder, "normal");
    normalFolder.mkdir();
    File file = new File(normalFolder, "file.txt");

    PrintWriter out = new PrintWriter(file);
    out.println("hello world");
    out.flush();
    out.close();
  }

  @Test
  public void testSomething() {
    createDirs();
    ....
  }
}

This works as well. EDIT if in the @Before method it looks like myfolder.create() needs called. And this is probably bad practice since the javadoc says not to call TemporaryFolder.create(). 2nd Edit Looks like you have to call the method to create the temp directories if you don't want them in the @Test methods. Also make sure you close any files you open in the temp directory or they won't be automatically deleted.

<imports excluded>

public class MyTest {

  @Rule
  public TemporaryFolder myfolder = new TemporaryFolder();

  private File otherFolder;
  private File normalFolder;
  private File file;

  public void createDirs() throws Exception {
    File tempFolder = myfolder.newFolder("folder");
    File normalFolder = new File(tempFolder, "normal");
    normalFolder.mkdir();
    File file = new File(normalFolder, "file.txt");

    PrintWriter out = new PrintWriter(file);
    out.println("hello world");
    out.flush();
    out.close();
  }

  @Test
  public void testSomething() {
    createDirs();
    ....
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文