Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 652
Jan Soltis Avatar asked Sep 23 '08 08:09

Jan Soltis


People also ask

How do I change the working directory in Eclipse?

In the Working directory section, click on the "Other" radio button. Click the "Workspace..." button and navigate to the desired working directory, typically the "bin" directory. Click "OK". Click "Apply" to save the changes and then proceed below to the next step to create the .

Where do I put JUnit tests in Eclipse?

To use JUnit you must create a separate . java file in your project that will test one of your existing classes. In the Package Explorer area on the left side of the Eclipse window, right-click the class you want to test and click New → JUnit Test Case. A dialog box will pop up to help you create your test case.

What is a launch configuration file in Eclipse?

In Eclipse, a launch configuration contains all the information to run or debug a program. A DS-5 debug launch configuration typically describes the target to connect to, the communication protocol or probe to use, the application to load on the target, and debug information to load in the debugger.


2 Answers

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.

like image 129
Henry B Avatar answered Sep 28 '22 11:09

Henry B


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.

like image 33
Jan Soltis Avatar answered Sep 28 '22 11:09

Jan Soltis