@DataJpaTest loads KafkaConfiguration and fails test

Viewed 80
@DataJpaTest
class DataJpaVerificationTest {
    @Autowired
    private JdbcTemplate template;

    @Test
    public void testTemplate() {
        assertThat(template).isNotNull();
    }
}

When I run this test I get the following error:

java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'kafkaConfig' defined in file [***\config\KafkaConfig.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.autoconfigure.kafka.KafkaProperties' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

where KafkaConfig class is a part of my application (app read data from Kafka and saves data to DB) and looks like:

@Configuration
@RequiredArgsConstructor
public class KafkaConfig {
    private final KafkaProperties kafkaProperties;

    @Bean
    public Properties consumerProperties() {
        Properties props = new Properties();
        props.putAll(this.kafkaProperties.buildConsumerProperties());
        return props;
    }
}

Based on information that I googled about DataJpaTest annotation:

created application context will not contain the whole context needed for our Spring Boot application, but instead only a “slice” of it containing the components needed to initialize any JPA-related components like our Spring Data repository.

So the question is: why Spring tries to load to the context Kafka specific bean for DataJpaTest?

0 Answers
Related