title: Test configuration for Jpa auditing
body: <p>I am trying to use Spring Data, Hibernate Envers and auditing in Spring Boot application. I have configured AuditorAwareImpl</p>
  
  <pre><code>public class AuditorAwareImpl implements AuditorAware&lt;String&gt; {
  
      @Override
      public Optional&lt;String&gt; getCurrentAuditor() {
          return Optional.of("Default auditor");
      }
  }
  </code></pre>
  
  <p>and configuration class for it.</p>
  
  <pre><code>@Configuration
  @EnableJpaAuditing(auditorAwareRef = "auditorProvider")
  public class AuditingConfiguration {
  
      @Bean
      public AuditorAware&lt;String&gt; auditorProvider() {
          return new AuditorAwareImpl();
      }
  }
  </code></pre>
  
  <p>Now I would like to create AuditorAware for my Integration tests. I have created new configuration class with test auditor</p>
  
  <pre><code>@Configuration
  @Profile("test")
  @EnableJpaAuditing(auditorAwareRef = "testAuditorProvider")
  public class TestAuditingConfiguration {
  
      @Bean
      @Primary
      public AuditorAware&lt;String&gt; testAuditorProvider() {
          return () -&gt; Optional.of("Test auditor");
      }
  
  }
  </code></pre>
  
  <p>And when I try to write my integration test like this</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest
  @ActiveProfiles("test")
  public class AuditingApplicationTests {
  
      @Autowired
      private AuditorAware&lt;String&gt; auditorAware;
  
      @Autowired
      private MovieRepository movieRepository;
  
      @Test
      public void testCurrentAuditor() {
          String currentAuditor = auditorAware.getCurrentAuditor().get();
          assertEquals("Test auditor", currentAuditor);
      }
  
      @Test
      public void movieRepositoryTest() {
          Movie movie = new Movie("Movie");
          movieRepository.save(movie);
  
          List&lt;Movie&gt; movies = movieRepository.findAll();
          Movie result = movies.get(0);
          assertEquals("Test auditor", result.getCreatedBy());
      }
  }
  </code></pre>
  
  <p>I am getting this error:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'jpaAuditingHandler' defined in null: Cannot register bean definition [Root bean: class [org.springframework.data.auditing.AuditingHandler]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] for bean 'jpaAuditingHandler': There is already [Root bean: class [org.springframework.data.auditing.AuditingHandler]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound.
  </code></pre>
  
  <p>When I remove <code>@EnableJpaAuditing</code> from <code>TestAuditingConfiguration</code> it works fine with one exception - autowired <code>auditorAware</code> in test is one from <code>TestAuditingConfiguration</code> but on the other hand for auditing is used from <code>AuditingConfiguration</code> so <code>result.getCreatedBy()</code> will return <code>Default auditor</code> instead of <code>Test auditor</code>. I read that for database integration tests should be used <code>@DataJpaTest</code> annotation so I have changed it. Now with enabled <code>@EnableJpaAuditing</code> on <code>TestAuditingConfiguration</code> I received:</p>
  
  <pre><code>org.springframework.beans.factory.UnsatisfiedDependencyException:  Unsatisfied dependency expressed through field 'auditorAware'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.domain.AuditorAware&lt;java.lang.String&gt;' available: expected at least 1 bean which qualifies as autowire candidate
  </code></pre>
  
  <p>But after adding <code>@Import(TestAuditingConfiguration.class)</code> it works as I excpected - <code>result.getCreatedBy()</code> returns <code>Test auditor</code>. So finally my test class looks like:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @DataJpaTest
  @ActiveProfiles("test")
  @Import(TestAuditingConfiguration.class)
  public class AuditingApplicationTests {
  
      @Autowired
      private AuditorAware&lt;String&gt; auditorAware;
  
      @Autowired
      private MovieRepository movieRepository;
  
      @Test
      public void testCurrentAuditor() {
          String currentAuditor = auditorAware.getCurrentAuditor().get();
          assertEquals("Test auditor", currentAuditor);
      }
  
      @Test
      public void movieRepositoryTest() {
          Movie movie = new Movie("Movie");
          movieRepository.save(movie);
  
          List&lt;Movie&gt; movies = movieRepository.findAll();
          Movie result = movies.get(0);
          assertEquals("Test auditor", result.getCreatedBy());
      }
  
  }
  </code></pre>
  
  <p>Now I am really confused how beans are used in specific profiles and how <code>@SpringBootTest</code> and <code>@DataJpaTest</code> works. And why <code>@Import</code> was important for <code>@DataJpaTest</code>? Can anyone explain me that and what is preferred approach for database testing?</p>