Use Eclipse project dir for Unit Test data

Use the default launch configuration of a unit test in Eclipse to access Unit Test data.

Use the default launch configuration of a unit test in Eclipse to access Unit Test data.

Best practice is to not access test data in relative or absolute paths in the file system. Instead to use classpath resources. But, maybe you have valid reasons to want to do things your own way. Thus, you want to get at test data in a folder within your project without monkeying around with adding file paths to launch configuration and all that stuff.

Ok, who am I to diminish your mojo. Just use the current directory that Eclipse creates. Example, you have this:

class FileCompareTest extends GroovyTestCase {
       /** 	 */
       public void testSomething(){
		def aFileName = 
                 "C:\path\to\projects\data\alpha.txt";
           // use file in test.
       }

Change it to:

class FileCompareTest extends GroovyTestCase{
       def basedir
	
       @Override
       protected void setUp() throws Exception {
           basedir = new java.io.File(".").getAbsolutePath()
       }
	
       /** 	 */
       public void testSomething(){			
           def aFileName = basedir + "dataalpha.txt"
       }

Updates

17JAN11: Could basedir = System.getProperty(“user.dir”); also be used?

Related links

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

Leave a Reply

Your email address will not be published. Required fields are marked *