Java 如何在 Eclipse 中默认 JUnit 启动配置的工作目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/119832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:29:18  来源:igfitidea点击:

How to default the working directory for JUnit launch configurations in Eclipse?

javaeclipsejuniteclipse-3.4

提问by Jan Soltis

Our Java code (not the test code) reads files from the current directory, which means the working directory needs to be set properly whenever we run the code.

我们的 Java 代码(不是测试代码)从当前目录读取文件,这意味着每当我们运行代码时都需要正确设置工作目录。

When launching a JUnit test from within Eclipse, a launch configuration automatically gets created. The problem is, that the working directory in that launch configuration is always by default the root project directory which is always wrong, the test fails, I have to open the launch configuration dialog, change the working directory and relaunch the test. This is very annoying. The same thing happens when I run a single test method.

从 Eclipse 内启动 JUnit 测试时,会自动创建启动配置。问题是,该启动配置中的工作目录默认始终是根项目目录,这总是错误的,测试失败,我必须打开启动配置对话框,更改工作目录并重新启动测试。这很烦人。当我运行单个测试方法时会发生同样的事情。

I've already considered these:

我已经考虑过这些:

  • Changing the current directory from the test code - not possible by design.
  • When opening a file, pass a parent directory parameter - too difficult, since this would affect lots of places.
  • Use the Copy launch configurationfeature of Eclipse to create new launch configurations from existing ones that already have a correct working directory set. This doesn't really makes sense here, since I would like to launch a test or a test method quickly, just by invoking "run this test / method as JUnit test".
  • 从测试代码更改当前目录 - 设计不可能。
  • 打开文件时,传递父目录参数 - 太难了,因为这会影响很多地方。
  • 使用Eclipse的复制启动配置功能,从已有正确工作目录集的现有配置中创建新的启动配置。这在这里没有意义,因为我想快速启动测试或测试方法,只需调用“将此测试/方法作为 JUnit 测试运行”。

All in all, it looks like it's responsibility of Eclipse, not the code.

总而言之,它看起来像是 Eclipse 的责任,而不是代码。

Is there a way to set the defaultworking directory for all future, newly createdJUnit launch configurations?

有没有办法为所有未来新创建的JUnit 启动配置设置默认工作目录?

采纳答案by Jan Soltis

As far as I can tell, there's no way of changing the default working directory for future JUnit launch configurations in Eclipse 3.4. This issue has also been reported as a bugin the Eclipse bug database.

据我所知,在 Eclipse 3.4 中,无法为未来的 JUnit 启动配置更改默认工作目录。此问题也已报告为Eclipse 错误数据库中的错误。

However, it is possible in IDEA. There's the Edit Defaultsbutton for setting all kinds of defaults for each launch config type separately.

但是,在 IDEA 中是可能的。有Edit Defaults按钮,用于分别为每个启动配置类型设置各种默认值。

回答by Henry B

This is a subjective answer:

这是一个主观的答案:

I believe you're doing your tests wrong, you shouldn't be loading the files from the JUnit using relative or complete paths, but instead, have them as resources in the project (add them to the build path) and load them as resources in the JUnit tests. This way if something changes on the filesystem, someone uses a different filesystem or IDE but still has them in the build path (as source folders) you're not going to have a problem.

我相信你的测试是错误的,你不应该使用相对或完整路径从 JUnit 加载文件,而是将它们作为项目中的资源(将它们添加到构建路径)并将它们作为资源加载在 JUnit 测试中。这样,如果文件系统上的某些内容发生了变化,有人使用了不同的文件系统或 IDE,但仍然在构建路径(作为源文件夹)中有它们,您就不会遇到问题。

I'm unsure if this is what you mean but if you really want to change it go to the Run Configuration options -> Your JUnit -> Arguments tab, at the bottom (Eclipse 3.4) you see Working Directory, click 'other' and change it from there.

我不确定这是否是您的意思,但如果您真的想更改它,请转到运行配置选项 -> 您的 JUnit -> 参数选项卡,在底部(Eclipse 3.4)您会看到工作目录,单击“其他”并从那里改变它。

回答by jiriki

I haven't found a possibility to do this, but what you can do is to use:

我还没有找到这样做的可能性,但你可以做的是使用:

getClass().getResourceAsStream(filename);

getClass().getResourceAsStream(文件名);

getClass().getClassLoader().getResourceAsStream(filename);

getClass().getClassLoader().getResourceAsStream(filename);

This methods locates a resource on the classpath. The first one is relative to the location of the class, the second one is relative to any classpath "root entry". You can then for example add the project root directory to the classpath.

此方法在类路径上定位资源。第一个是相对于类的位置,第二个是相对于任何类路径“根条目”。例如,您可以将项目根目录添加到类路径中。

This does not work however if you want to write to a file as well.

但是,如果您也想写入文件,这将不起作用。

回答by Daniel Fanjul

If your tests depend on the current working directory, I think it is responsability of your tests to setup correctly that working directory, and to configure your classes to be tested to point that directory.

如果您的测试依赖于当前的工作目录,我认为您的测试有责任正确设置该工作目录,并将要测试的类配置为指向该目录。

If you have a superclass for most of your tests, write a constant within it. Or: if you have a superclass for most of your tests, write a @Before setup method. Or: if you have not a superclass for most of your tests, write a constant in some class of the testing codebase.

如果您的大多数测试都有一个超类,请在其中编写一个常量。或者:如果您的大多数测试都有一个超类,请编写一个 @Before 设置方法。或者:如果您的大多数测试都没有超类,请在测试代码库的某个类中编写一个常量。

回答by Jan Soltis

With Eclipse 3 you can set you working directory. Go to you run/debug configuration -> Arguments tab. In the Working directory select "other" and enter the root of your test

使用 Eclipse 3,您可以设置工作目录。转到运行/调试配置 -> 参数选项卡。在工作目录中选择“其他”并输入测试的根目录