0

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

vach
  • 10,571
  • 12
  • 68
  • 106
  • I fear this is a bit too broad to answer. Some advice: Resources in `src/main/resources` will end up in the generated JAR. That also means that they cannot be changed. As a consequence, this is not the correct location for changeable configuration files. It should only contain _static_ resources for your program/library (for example: images). The same applies to `src/test/resources`. Here you should only place resource files for your tests. Configuration files should be elsewhere. We usually load them via system properties. – Seelenvirtuose Apr 20 '15 at 06:43
  • Well what about log4j.config or spring context xmls? you're holding them elseware? – vach Apr 20 '15 at 06:46
  • Isnt there any option to force junit to pick one in test fork if 2 files are available with the same name? Simple feature like that could solve so much hussle... – vach Apr 20 '15 at 06:47
  • A log4j configuration file is exactly what I am speaking about. If you put it into `src/main/resources` how could you possibly change the log configuration afterwards? Worse, if you do that with several projects, you end up with several log4j configurations in your application. Ironically, log4j2 (!) [provides a way to deal with that](http://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticConfiguration). AFAIK, Spring allows to specify the context resource in your test classes (`@ContextConfiguration`?). – Seelenvirtuose Apr 20 '15 at 06:54
  • Yes i use @ContextConfiguration but you dont specify test or main in there you just give it something like "/myconfig.xml" and it somehow figures what to pick... Someone wrote in other question that junit favours the test package if 2 files are in class path (altough i cant undesrtand how it could possibly figure it out...) – vach Apr 20 '15 at 07:06
  • 1
    The resources in the _test directory_ (`src/test/resources`) should have other names. – Seelenvirtuose Apr 20 '15 at 07:08
  • @Seelenvirtuose is it possible to somehow change junit behaviour and make it not to include main/resources at all?... Like its far more natural for me to copy static resources from main/resources rather than rename every single file i want to use in tests... – vach Apr 20 '15 at 07:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75680/discussion-between-vach-and-seelenvirtuose). – vach Apr 20 '15 at 07:10
  • possible duplicate of [Specifying a custom log4j.properties file for all of JUnit tests run from Eclipse](http://stackoverflow.com/questions/24231773/specifying-a-custom-log4j-properties-file-for-all-of-junit-tests-run-from-eclips) – Raedwald Apr 20 '15 at 07:51

1 Answers1

1

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.

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66