Ilnyr Nasibullin
Ilnyr Nasibullin

Reputation: 1

Cannot register bean definition for bean that is already defined in TestConfiguration

I have a DataJpaTest with testcontainers for testing my database repositories, such as

@DataJpaTest  
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)  
@Testcontainers 
@ActiveProfiles("test")  
class OrderRepositoryTest

I'm using @ActiveProfiles("test") to be able to config my test application via application-test.yml

Also I have a test configuration class for entities and repositories

@SpringBootConfiguration
@EnableJpaRepositories(basePackages = {
        "com.sample.logistics"
})
@EntityScan(basePackages = {
        "com.sample.logistics"
})
@ActiveProfiles("test")
public class TestConfig {

    @Value("${spring.datasource.username}")
    private String userName;

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.schema}")
    private String schema;

    @Bean
    public DataSource getTestDataSource() {
        DriverManagerDataSource dataSourceBuilder = new DriverManagerDataSource();
        dataSourceBuilder.setUrl(url);
        dataSourceBuilder.setSchema(schema);
        dataSourceBuilder.setUsername(userName);
        dataSourceBuilder.setPassword(password);
        return dataSourceBuilder;
    }

}

Tests are working well, but I have a problem with launching my main Spring Boot Application, it falls with the following problem.

Cannot register bean definition for bean 'orderRequestTemplateRepository' since there is already [Root bean: class ; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null; defined in com.sample.logistics.order_requests.dao.repository.OrderRequestTemplateRepository defined in @EnableJpaRepositories declared on TestConfig] bound.

Seems like some beans were registered by my TestConfig class, but I have no idea, why did TestConfig actually work here. I was expecting that it will be ignored because it's located in src.test.java package.

How can I ignore TestConfig in my main application?

I tried to set parameter spring.main.allow-bean-definition-overriding=true, but it didn't work.

With this parameter I had the following problem: ` Parameter 0 of constructor in com.sample.logistics.order_requests.service.OrderRequestServiceImpl required a bean named 'entityManagerFactory' that could not be found.

Consider defining a bean named 'entityManagerFactory' in your configuration.`

Upvotes: 0

Views: 775

Answers (1)

J.Solongo
J.Solongo

Reputation: 61

Check your spring application's main class to see if the @ComponentScan includes the path of the test package as well. Such as:

@ComponentScan(basePackages = {"com.sf.slg.*"})

and your test path just happens to be com.sf.slg.test.xxx

Upvotes: 0

Related Questions