1

我在 Mac 10.7.4 上运行 Eclipse Indigo。我在 Eclipse 中设置了一个 Maven (3.0.3) 项目,但是当我从 Eclipse 中运行 JUnit 测试时,它无法在我的 src/main/resources 目录中加载属性文件。但是,在 Maven 命令行上运行相同的测试通过了,所以我想知道我的设置有什么问题。这是测试......</p>

@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

以下是我加载属性文件的方式……</p>

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

当我通过 Eclipse 运行测试时(通过右键单击 Package Explorer 中的 Java 测试文件并转到 Run As -> JUnit Test),它会因这个异常而死掉……</p>

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)

怎么了?

4

2 回答 2

4

确保 'src/main/resources' 目录位于您的 eclipse JUnit 测试类路径中(请参阅 参考资料Run configuration -> classpath tab)。

于 2012-06-16T20:26:55.387 回答
2

请使用以下代码:-

 this.getClass().getResourceAsStream(propertiesFile);

请不要使用“ getClassLoader() ”。

我希望这可能会有所帮助。

于 2012-06-17T17:32:09.447 回答