Can't load ResourceBundle during junit test
Asked Answered
B

2

5

I'm trying to write some Junit tests to test old classes in our app. The code is trying to load a ResourceBundle (for translations) but fails. I guess the problem is classpath related but I can't figure it out.

The code is laid out in /src and my tests are in /test. The ResourceBundles are loaded given a base name relative to /src, say, "foo/bar/baz".

My tests use the same classpath as the app itself, so I don't understand why it won't find the bundles.

Any ideas to what's wrong?

Bunn answered 16/9, 2010 at 13:30 Comment(2)
Are your tests under a directory called /test?Tress
If you temporarily change the relative path your ResourceBundles to an absolute one, does the problem go away?Tress
U
8

Resources are not resolved from the source folder but from the class directory. Are your resource files copied to the output folder (bin, target/classes etc.) ? If not, your classes can't find them.

Uninspired answered 16/9, 2010 at 13:52 Comment(1)
D'uh! Of course, my tests use another output directory than the app does. This is definately it.Bunn
C
9

If you are using maven, you typically want to put all kind of properties files and resource bundles in a separate source folder called src/main/resources/.

If you want to override values in your unit tests then you can add a duplicate prop. file in the src/test/resources source folder.

Culverin answered 19/11, 2012 at 10:1 Comment(0)
U
8

Resources are not resolved from the source folder but from the class directory. Are your resource files copied to the output folder (bin, target/classes etc.) ? If not, your classes can't find them.

Uninspired answered 16/9, 2010 at 13:52 Comment(1)
D'uh! Of course, my tests use another output directory than the app does. This is definately it.Bunn

© 2022 - 2024 — McMap. All rights reserved.