简体   繁体   中英

Junit to use correct resources

When writing unit tests for the application, usually I create copy of some configurations that are in original main/resources and modify them for test purpose, but i leave the names the same. However Junit sometimes takes the one in src and sometimes the one in the test/resources .

How do we manage which one it picks up without renaming files?

For example i have some "config.json" which is in both test and main's resources, how does junit choose which one to pick when running a test...

Ok, just a quick answer:

You cannot exclude resources from src/main/resources . They are always inside your class path and thus they are part of your program/library.

But you shouldn't have to do so.

Log4j2 for example allows to have a differently named configuration file in the class path (while running the tests). Just name it log4j2-test.xml and it will be loaded prior to a non-test file.

Spring behaves the same when annotating the test classes with @ContextConfiguration .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM