Failing to load properties file in an Eclipse - JUnit test

I'm running Eclipse Indigo on Mac 10.7.4. I have a Maven (3.0.3) project set up in Eclipse, but when I run a JUnit test from within Eclipse, it is failing to load a properties file in my src/main/resources directory. However, running the same test on the Maven command line passes, so I'm wondering what's wrong with my setup. Here's the test …

public void setUp() throws IOException { 
    stateService = new StateService("");
}   // setUp

public void testPositive() { 
    final String stateName = stateService.getStateFromAbbrev("TX");
    Assert.assertEquals("Texas", stateName);
}   // testPositive

and here's how I load the properties file …

public StateService(final String propertiesFile) {
    abbrevToWordsProps = new Properties();
    InputStream in = null;
        in = this.getClass().getClassLoader().getResourceAsStream(propertiesFile);
    } catch (IOException e) {
        // TODO Auto-generated catch block
    } finally {
        if (in != null) { 
            try {
            } catch (IOException e) {
        }   // if

when I run the test through Eclipse (by right-clicking on the Java test file in Package Explorer and going to Run As -> JUnit Test), it dies with this Exception …

    at java.util.Properties$LineReader.readLine(
    at java.util.Properties.load0(
    at java.util.Properties.load(
    at org.mainco.myco.dido.service.StateService.<init>(
    at org.mainco.myco.dido.test.AbstractDIDOTest.setUp(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(
    at org.junit.internal.runners.statements.RunBefores.evaluate(
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(
    at org.junit.runners.ParentRunner$
    at org.junit.runners.ParentRunner$1.schedule(
    at org.junit.runners.ParentRunner.runChildren(
    at org.junit.runners.ParentRunner.access$000(
    at org.junit.runners.ParentRunner$2.evaluate(
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(

What's going wrong?


Be sure that the 'src/main/resources' directory is on your eclipse JUnit test classpath (see Run configuration -> classpath tab).

Please use the following code: -


Please do not use the "getClassLoader()".

I hope this may help.

Need Your Help

Is it possible to protect a single element in the appSettings section instead of the entire section?

c# .net app-config

I would like to protect one key/value pair in my appSettings but not the others using something like I've previously done with the ProtectSection method as seen below.

How can I call my own system call in a exit() system call

linux operating-system kernel linux-kernel

I've written new system call that can terminate all the children of a given process, and I want to use this system call when user calls the "exit()" system call.