Typesafe Config: Load configuration from src/test/resources

19.9k views Asked by At

This is a beginner question. So my app structure looks like

src/main/java/...
src/main/resources/application.conf

src/test/java/...
src/test/resources/module/test.module.conf

application.conf

location: mainLocation

test.module.conf

location: testLocation

In my test, I do

  @Test
  public void testLoadConfig() {
    final Config config = ConfigFactory.parseResources("test.module.conf");
    System.out.println(config);
  }

and what I see

Config(SimpleConfigObject({}))

Surely something is not right, but I can't spot it

UPDATE

When I do just

  @Test
  public void testActorForFailure() {
//    final Config config = ConfigFactory.load("test.module.conf");
    final Config config = ConfigFactory.load();
    System.out.println(config.getString("location"));

  }

I see

mainLocation

So overriding is not working, why?

2

There are 2 answers

0
cmbaxter On BEST ANSWER

If you want to load that test config file try this:

ConfigFactory.load("modules/test.module")

The base ConfigFactory.load() method looks to load 'application.conf'. If you want it to load a different file you need to tell it what that different file is.

0
z atef On

To load from test-resource-root:

f.ex:

someModule/src/test/resources/some.conf

This works for me:

ConfigParseOptions options = ConfigParseOptions.defaults().setAllowMissing(false);
        
final Config config =  ConfigFactory.parseResources("some.conf", options);