title: AutoConfigureTestDatabase with multiple DataSource beans
body: <p>I have two DataSources in my application, which work as expected when connecting to the databases in a live environment. However, when writing unit tests for this application, I'm encountering problems with my bean definitions.</p>
  
  <p>This is my primary DataSource configuration:</p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories(
          basePackages = "foo.bar.repository.primary",
          entityManagerFactoryRef = "primaryEntityManager",
          transactionManagerRef = "primaryTransactionManager")
  public class PrimaryDataSourceConfig {
  
      @Bean
      @Primary
      @ConfigurationProperties("primary.datasource")
      public DataSource primaryDataSource() {
          return DataSourceBuilder.create().build();
      }
  
      @Bean
      @Primary
      public LocalContainerEntityManagerFactoryBean primaryEntityManager() {
  
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(primaryDataSource());
          em.setPackagesToScan("foo.bar.domain.entity");
  
          HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setJpaVendorAdapter(vendorAdapter);
  
          Map&lt;String, String&gt; properties = new HashMap&lt;&gt;();
          properties.put("hibernate.implicit_naming_strategy",
                  "org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy");
          properties.put("hibernate.physical_naming_strategy",
                  "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
          em.setJpaPropertyMap(properties);
  
          return em;
      }
  
      @Bean
      @Primary
      public PlatformTransactionManager primaryTransactionManager() {
  
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(primaryEntityManager().getObject());
          return transactionManager;
      }
  }
  
  </code></pre>
  
  <p>I have a test where all I want to do is spin up the entire application context, looking like this.</p>
  
  <pre><code>@AutoConfigureTestDatabase
  @RunWith(SpringRunner.class)
  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  public class ErpApplicationTest {
  
      @Test
      public void test() {
          // Application started
      }
  }
  </code></pre>
  
  <p>However, when I run this test, I get the following error:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'primaryEntityManager' defined in class path resource [foo/bar/primaryDataSourceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'primaryEntityManager' threw exception; nested exception is java.lang.IllegalArgumentException: No visible constructors in class org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration$EmbeddedDataSourceFactoryBean
  </code></pre>
  
  <p>What is causing this error, and what can I do to rectify it? Flyway is able to connect to the embedded H2 instance the annotation generates and run its migrations, but the primary DataSource, alongside its EntityManager fail at creation.</p>