Additional datastore registration (for testing)

Hi,

I have an additional datastore created, and works Ok on the UI.

But when I want to implement some testing on the store Jmix complains “Store XXXX is not registered”.

How does one register the additonal datastore on the XXXTestConfiguration class? note that I need it to pull the properties from the file (as the StoreConfiguration class does)

I have been looking at the source code trying to understand this, so far I found things like this:


    @Bean
    DataSource db1DataSource() {
        return new EmbeddedDatabaseBuilder()
                .generateUniqueName(true)
                .setType(EmbeddedDatabaseType.HSQL)
                .build();
    }

Which works for registering data sources with the EmbeddedDatabase.

That gave me the idea of doing something like…:


    @Bean
    DataSource db1DataSource(MyStoreConfiguration myStoreConfiguration) {
        // instantiate the dataSource
    }

But it fails miserably complaining that no beans can provide MyStoreConfiguration (which is on MyStoreConfiguration.class next to the main Configuration class.

I see I could instantiate the DataSource by hand like here but I need the Configuration class to read the properties file (I want to put in some integration tests that make sure the MSSQL database works as expected)

So… still stuck not knowing how to register my additional dataSource for testing :frowning:

Why do you need another configuration class?
What about creating src/test/resources/application-test.properties file and using test profile when running tests like this:

@SpringBootTest
@ActiveProfiles("test")
public class MyTest {
  //...
}

Regards,
Konstantin

By the MyStoreConfiguration.class class I was referring to the class that Studio creates in the main package to load the datastore configuration. Not an additional class. Full class bellow (collapsed)

MyStoreConfiguration.class
@Configuration
public class MyStoreConfiguration {

    @Bean
    @ConfigurationProperties("my.datasource")
    DataSourceProperties myDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties(prefix = "my.datasource.hikari")
    DataSource myDataSource(@Qualifier("myDataSourceProperties") DataSourceProperties properties) {
        return properties.initializeDataSourceBuilder().build();
    }

    @Bean
    LocalContainerEntityManagerFactoryBean myEntityManagerFactory(
            @Qualifier("myDataSource") DataSource dataSource,
            JpaVendorAdapter jpaVendorAdapter,
            DbmsSpecifics dbmsSpecifics,
            JmixModules jmixModules,
            Resources resources) {
        return new JmixEntityManagerFactoryBean("my", dataSource, jpaVendorAdapter, dbmsSpecifics, jmixModules, resources);
    }

    @Bean
    JpaTransactionManager myTransactionManager(@Qualifier("myEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JmixTransactionManager("my", entityManagerFactory);
    }
}

By doing that the application context loads without complaining about the dataStore not being registered. But then when I try to use it I get: No bean named ‘myStoreTransactionManager’ available

It is as if the aforementioned configuration class (which provides the dataStore beans) is never loaded. But when running the application it is there.

I should also mention that this is an addon module, not application module.

Thx,
Marc

It suddently started working.
I’ll try to find out why/how :woman_shrugging:

Did you find out what was the reason?

I have been unable to reproduce. Probably some caching issue