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

前端 未结 3 1457
渐次进展
渐次进展 2021-01-30 20:31

I am having a problems running the following code:

configService.setMainConfig(\"src/test/resources/MainConfig.xml\");

From within a Junit @Bef

3条回答
  •  情歌与酒
    2021-01-30 21:05

    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().

提交回复
热议问题