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 …

@Before
public void setUp() throws IOException { 
    ...
    stateService = new StateService("states.properties");
    ...
}   // setUp

@Test
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;
    try{
        in = this.getClass().getClassLoader().getResourceAsStream(propertiesFile);
        abbrevToWordsProps.load(in);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace(System.err);
    } finally {
        if (in != null) { 
            try {
                in.close();
            } 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 …

java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:418)
    at java.util.Properties.load0(Properties.java:337)
    at java.util.Properties.load(Properties.java:325)
    at org.mainco.myco.dido.service.StateService.<init>(StateService.java:16)
    at org.mainco.myco.dido.test.AbstractDIDOTest.setUp(AbstractDIDOTest.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

What's going wrong?

Answers


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: -

 this.getClass().getResourceAsStream(propertiesFile);

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.