Skip to content

application-test.yaml seems to be ignored when test makes second call to DatabaseConfig.loadFromProperties #2997

Description

@simontankersley

Background

Often when testing in Spring, multiple application contexts can be created and destroyed. When using programatic configuration of ebeans this could create a situation where multiple calls are made to DatabaseConfig.loadFromProperties method.

Expected behavior

It doesn't matter how many times the tests call DatabaseConfig.loadFromProperties, the test configuration is found and used each time.

Actual behavior

Only the first call to DatabaseConfig.loadFromProperties succeeds. When trying to use a Database created from the DatabaseConfig that was created by a second call to DatabaseConfig.loadFromProperties we get exception:

    io.ebean.datasource.DataSourceConfigurationException: DataSource user is not set? url is [null]
        at app//io.ebean.datasource.pool.ConnectionPool.<init>(ConnectionPool.java:121)
        at app//io.ebean.datasource.pool.ConnectionPoolFactory.createPool(ConnectionPoolFactory.java:14)
        at app//io.ebean.datasource.DataSourceFactory.create(DataSourceFactory.java:25)
        at app//io.ebeaninternal.server.core.InitDataSource.create(InitDataSource.java:113)
        at app//io.ebeaninternal.server.core.InitDataSource.createFromConfig(InitDataSource.java:104)
        at app//io.ebeaninternal.server.core.InitDataSource.initDataSource(InitDataSource.java:44)
        at app//io.ebeaninternal.server.core.InitDataSource.initialise(InitDataSource.java:33)
        at app//io.ebeaninternal.server.core.InitDataSource.init(InitDataSource.java:24)
        at app//io.ebeaninternal.server.core.DefaultContainer.setDataSource(DefaultContainer.java:220)
        at app//io.ebeaninternal.server.core.DefaultContainer.createServer(DefaultContainer.java:86)
        at app//io.ebeaninternal.server.core.DefaultContainer.createServer(DefaultContainer.java:29)
        at app//io.ebean.DatabaseFactory.createInternal(DatabaseFactory.java:136)
        at app//io.ebean.DatabaseFactory.create(DatabaseFactory.java:85)
        at app//org.example.PetTest.testDouble(PetTest.java:44)

It appears as though the test configuration isn't found.

Steps to reproduce

I've created a sample project with code at https://github.com/simontankersley/programatic-ebean-test

This code fails

        // simulate using a custom object mapper
        ObjectMapper mapper = new ObjectMapper();

        // simulate creating a database configuration twice
        // this often happens when using spring tests where several
        // contexts can be created and destroyed - causing multiple
        // calls to create the database configuration from properties
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.loadFromProperties();
        dbConfig.setObjectMapper(mapper);
        Database db = DatabaseFactory.create(dbConfig);

        dbConfig = new DatabaseConfig();
        dbConfig.loadFromProperties();
        dbConfig.setObjectMapper(mapper);
        db = DatabaseFactory.create(dbConfig);
        db.save(new Pet());

If you want to see a base line vs a failing test see below

Success when running a test with a single call to DatabaseConfig.loadFromProperties

git clone git@github.com:simontankersley/programatic-ebean-test.git
cd programatic-ebean-test
./gradlew test --tests '*testSingle'

gives

> Task :test

PetTest > testSingle() PASSED

Failure when running a test with a multiple calls to DatabaseConfig.loadFromProperties

./gradlew test --tests '*testDouble'

gives

> Task :test

PetTest > testDouble() FAILED
    io.ebean.datasource.DataSourceConfigurationException: DataSource user is not set? url is [null]
        at app//io.ebean.datasource.pool.ConnectionPool.<init>(ConnectionPool.java:121)
        at app//io.ebean.datasource.pool.ConnectionPoolFactory.createPool(ConnectionPoolFactory.java:14)
        at app//io.ebean.datasource.DataSourceFactory.create(DataSourceFactory.java:25)
        at app//io.ebeaninternal.server.core.InitDataSource.create(InitDataSource.java:113)
        at app//io.ebeaninternal.server.core.InitDataSource.createFromConfig(InitDataSource.java:104)
        at app//io.ebeaninternal.server.core.InitDataSource.initDataSource(InitDataSource.java:44)
        at app//io.ebeaninternal.server.core.InitDataSource.initialise(InitDataSource.java:33)
        at app//io.ebeaninternal.server.core.InitDataSource.init(InitDataSource.java:24)
        at app//io.ebeaninternal.server.core.DefaultContainer.setDataSource(DefaultContainer.java:220)
        at app//io.ebeaninternal.server.core.DefaultContainer.createServer(DefaultContainer.java:86)
        at app//io.ebeaninternal.server.core.DefaultContainer.createServer(DefaultContainer.java:29)
        at app//io.ebean.DatabaseFactory.createInternal(DatabaseFactory.java:136)
        at app//io.ebean.DatabaseFactory.create(DatabaseFactory.java:85)
        at app//org.example.PetTest.testDouble(PetTest.java:44)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions