我在Mac 10.7.4上运行Eclipse Indigo。我在Eclipse中设置了一个Maven(3.0.3)项目,但是当我在Eclipse中运行JUnit测试时,它无法在我的src / main / resources目录中加载属性文件。但是,在Maven命令行上运行相同的测试会通过,所以我想知道我的设置有什么问题。这是测试...
@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
以下是我加载属性文件的方法......
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),它会因此异常而死...
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)
出了什么问题?
答案 0 :(得分:4)
确保'src / main / resources'目录位于eclipse JUnit测试类路径中(参见Run configuration -> classpath tab
)。
答案 1 :(得分:1)
请使用以下代码: -
this.getClass().getResourceAsStream(propertiesFile);
请不要使用“ getClassLoader() ”。
我希望这可能有所帮助。