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

5

24

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.

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.

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".

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

Is there a way to set the default working directory for all future, newly created JUnit launch configurations?

Hofmann answered 23/9, 2008 at 8:26 Comment(0)
H
10

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.

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

Hofmann answered 13/10, 2008 at 16:7 Comment(1)
It might be possible for moreunit to work around this also. I've request a feature request for that here - sourceforge.net/tracker/?group_id=156007&atid=798059.Justify
N
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.

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.

Nastassia answered 23/9, 2008 at 8:48 Comment(1)
What if you want to set the CWD as target/my-test-loc but target doesn't exist yet?Clouet
H
10

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.

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

Hofmann answered 13/10, 2008 at 16:7 Comment(1)
It might be possible for moreunit to work around this also. I've request a feature request for that here - sourceforge.net/tracker/?group_id=156007&atid=798059.Justify
T
3

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

Teide answered 3/2, 2009 at 13:57 Comment(0)
M
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.

Masters answered 23/9, 2008 at 8:53 Comment(0)
C
0

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.

Cwmbran answered 23/9, 2008 at 9:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.