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

时间:2023-02-05 11:50:10

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 configuration feature 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 default working directory for all future, newly created JUnit launch configurations?

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

5 个解决方案

#1


11  

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 - > Arguments选项卡,在底部(Eclipse 3.4)你看到工作目录,点击'其他'和从那里改变它。

#2


9  

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 bug in the Eclipse bug database.

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

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

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

#3


2  

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,您可以设置工作目录。转到运行/调试配置 - >参数选项卡。在工作目录中选择“其他”并输入测试的根目录

#4


1  

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

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

getClass().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.

但是,如果您还想写入文件,则不起作用。

#5


1  

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安装方法。或者:如果您的大多数测试都没有超类,请在测试代码库的某个类中编写一个常量。

#1


11  

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 - > Arguments选项卡,在底部(Eclipse 3.4)你看到工作目录,点击'其他'和从那里改变它。

#2


9  

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 bug in the Eclipse bug database.

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

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

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

#3


2  

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,您可以设置工作目录。转到运行/调试配置 - >参数选项卡。在工作目录中选择“其他”并输入测试的根目录

#4


1  

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

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

getClass().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.

但是,如果您还想写入文件,则不起作用。

#5


1  

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安装方法。或者:如果您的大多数测试都没有超类,请在测试代码库的某个类中编写一个常量。