Wednesday, October 30, 2013

JUnit + Eclipse + Maven = fun

Given:

/src/test/java/com/helloworld/a.xml with body <src_test_java/>
/src/test/resources/com/helloworld/a.xml with body <src_test_resources/>
/src/main/java/com/helloworld/a.xml with body <src_main_java/>
/src/main/resources/com/helloworld/a.xml with body <src_main_resources/>

Run the following JUnit(NOT under /src/test/java/com/helloworld/) in Eclipse configured by Maven with Eclipse plugin (mvn eclipse:eclipse):

A: this.getClass().getResource("a.xml”);
B: this.getClass().getResource("/com/helloworld/a.xml");
C: this.getClass().getResourceAsStream("a.xml");
D: this.getClass().getResourceAsStream("/com/helloworld/a.xml");
E: this.getClass().getClassLoader().getResource("a.xml");
F: this.getClass().getClassLoader().getResource("/com/helloworld/a.xml");
G: this.getClass().getClassLoader().getResourceAsStream("a.xml");
H: this.getClass().getClassLoader().getResourceAsStream("/com/helloworld/a.xml");

What should the result look like?

Answer:
A: empty string
B: <src_test_resources/>
C: empty string
D: <src_test_resources/>
E: null
F: null
G: null
H: null