views:

3479

answers:

6

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?

+4  A: 

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.

PintSizedCat
A: 

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.

jiriki
A: 

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.

Banengusk
+1  A: 

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.

Jan Soltis
A: 

I have the similar problem - I need to change some run configurations every time I want to run one of my tests, I have very many tests, so for each of them I have to go to the Run Configuration options -> My JUnit -> ... and change necessary arguments manually. Is there a way to do it not manually, for example by writing Eclipse plugin or by providing some settings?

A: 

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