vach
vach

Reputation: 11377

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

Upvotes: 0

Views: 382

Answers (1)

Seelenvirtuose
Seelenvirtuose

Reputation: 20608

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.

Upvotes: 1

Related Questions