Mick Mick - 1 year ago 83
Java Question

Why Can't I access src/test/resources in Junit test run with Maven?

I am having a problems running the following code:


From within a Junit @Before method.

Is this the way Maven builds out its target folder?

Answer Source

Access MainConfig.xml directly. The src/test/resources directory contents are placed in the root of your CLASSPATH.

More precisely: contents of src/test/resources are copied into target/test-classes, so if you have the following project structure:

└── src
    └── test
        ├── java
        │   └── foo
        │       └── C.java
        └── resources
            ├── a.xml
            └── foo
                └── b.xml

It will result with the following test CLASSPATH contents:

  • /foo/C.class
  • /a.xml
  • /foo/b.xml

To actually access the files from Java source, use getClass().getResource("/MainConfig.xml").getFile().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download