What I am trying to achieve:
Run an integration test for a Spring Controller in a test container, use a TestJpaConfig instead of JpaConfig:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@Testcontainers
@ComponentScan(excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = JpaConfig.class)
})
@Import(TestJpaConfig.class)
public class SomeControllerIT {
@Container
static PostgreSQLContainer<?> postgresContainer = new PostgreSQLContainer...
@DynamicPropertySource
static void overrideProperties(DynamicPropertyRegistry registry) {
...
}
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@MockBean
Bean beans...
@Autowired
Class classes...
@BeforeEach
void setup() {
...
}
@Test
void test_Something() throws Exception {
...
}
}
I am having this error:
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=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=from; initMethodNames=null; destroyMethodNames=null] for bean 'jpaAuditingHandler' since there is already [Root bean: class [org.springframework.data.auditing.AuditingHandler]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=from; initMethodNames=null; destroyMethodNames=null] bound.
My JpaConfig:
@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class JpaConfig {
@Bean
public AuditorAware<Long> auditorProvider() {
return new SpringSecurityAuditorAware();
}
}
And my TestJpaConfig:
@TestConfiguration
@EnableJpaAuditing(auditorAwareRef = "testAuditorProvider")
public class TestJpaConfig {
@Bean(name = "test")
public AuditorAware<Long> auditorProvider() {
return () -> Optional.of(1L);
}
}
I've tried to work to fix this error without success, asking here for help now, any ideas? I have limited knowledge in Spring.