我在Mac 10.7.4上運行Eclipse Indigo。我在Eclipse中設置了Maven(3.0.3)項目,但是當我在Eclipse中運行JUnit測試時,未能在我的src/main/resources目錄中加載屬性文件。然而,在Maven命令行上運行相同的測試通過,所以我想知道我的設置有什麼問題。這裏的測試...未能在Eclipse中加載屬性文件--JUnit測試
@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)
怎麼回事?
right ...或者運行'mvn eclipse:eclipse'並在項目根目錄上按F5,這是實現完全相同的事情的另一種方法:) – mschonaker
@mschonaker我懷疑混合使用Maven Integration(m2e)與maven-eclipse-plugin。 – FrVaBe