title: Spring Boot test configuration for multiple datasources
body: <p>So I am trying to introduce some unit tests for my Spring Boot application and I am having a hard time trying to setup the config for the test environment. My application is configured to connect to two different Postgres databases as follows:</p>
  <p><strong>application.properties</strong></p>
  <pre><code>spring.db1-datasource.jdbc-url= jdbc:postgresql://localhost:5432/db1
  spring.db1-datasource.username= admin
  spring.db1-datasource.password= admin
  spring.db1-datasource.driverClassName= org.postgresql.Driver
  
  spring.db2-datasource.jdbc-url= jdbc:postgresql://localhost:5432/db2
  spring.db2-datasource.username= admin
  spring.db2-datasource.password= admin
  spring.db2-datasource.driverClassName= org.postgresql.Driver
  </code></pre>
  <p><strong>FirstDbConfig.java</strong></p>
  <pre><code>@Configuration
  @PropertySource({&quot;classpath:application.properties&quot;})
  @EnableJpaRepositories(
          basePackages = &quot;org.myapp.database.db1.repository&quot;,
          entityManagerFactoryRef = &quot;firstEntityManager&quot;,
          transactionManagerRef = &quot;firstTransactionManager&quot;)
  public class FirstDbConfig {
      @Autowired
      private Environment env;
  
      public FirstDbConfig() {
          super();
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean firstEntityManager() {
          final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(firstDataSource());
          em.setPackagesToScan(&quot;org.myapp.database.db1&quot;);
  
          final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setJpaVendorAdapter(vendorAdapter);
          final HashMap&lt;String, Object&gt; properties = new HashMap&lt;String, Object&gt;();
          properties.put(&quot;hibernate.hbm2ddl.auto&quot;, env.getProperty(&quot;hibernate.hbm2ddl.auto&quot;));
          properties.put(&quot;hibernate.dialect&quot;, env.getProperty(&quot;hibernate.dialect&quot;));
          em.setJpaPropertyMap(properties);
  
          return em;
      }
  
      @Bean
      @ConfigurationProperties(prefix=&quot;spring.db1-datasource&quot;)
      public DataSource firstDataSource() {
          return DataSourceBuilder.create().build();
      }
  
      @Bean
      public PlatformTransactionManager firstTransactionManager() {
          final JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(firstEntityManager().getObject());
          return transactionManager;
      }
  }
  
  </code></pre>
  <p><strong>SecondDbConfig.java</strong></p>
  <p>Not including it because it's quite similar. Targets <code>org.myapp.database.db2.repository</code></p>
  <p>For my basic test, I am trying to test a simple service that injects one of these configured repositories. This looks like that:</p>
  <p><strong>MyServiceTest.java</strong></p>
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringBootTest(classes = MyService.class)
  @ContextConfiguration(
          classes = {FirstDbConfig.class, SecondDbConfig.class },
          loader= AnnotationConfigContextLoader.class
  )
  public class MyServiceTest {
  
      @InjectMocks
      private MyService myService;
  
      @Mock
      private FirstDbRepository dbRepository;
  
      @Test
      public void test() {
          ...
          // call to myService.method()
      }
  }
  
  </code></pre>
  <p>Test fails with:</p>
  <pre><code>java.lang.IllegalStateException: Failed to load ApplicationContext
  ...
  Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set at ...
  </code></pre>
  <p>It obviously has to do with <code>hibernate.dialect</code> not being set but no idea why that is the case. Any ideas?</p>