Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

java - Why bean is not found in Spring Boot test?

I have a Spring Boot Application, its file hierarchy:

src/
└── main/
    └── java/
    |      ├── com.example/
    |      |         ├── config/ 
    |      |         |     └──R2DBCConfiguration.java
    |      |         ├── dao/
    |      |         |     └── UnitR2DBCRepository.java
    |      |         └── R2DBCApplication.java
    |      └──resources/
    |             └── application.yml
    └── test
        └── java/
               ├── com.example/
               |         └── dao/
               |               └── R2DBCTest.java
               └──resources/
                      └── application.yml

R2DBCConfiguration.java

@Configuration
@AllArgsConstructor
public class R2DBCConfiguration {
    @Bean
    public ConnectionFactory connectionFactory() {
        return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder(). ... .build());
    }

    @Bean
    public DatabaseClient databaseClient() {
        return DatabaseClient.builder().connectionFactory(connectionFactory()).build();
    }
}

Unit2RDBCRepository.java

@Repository
public interface UnitR2DBCRepository extends ReactiveCrudRepository<Unit, UUID> { }

R2DBCApplication.java

@SpringBootApplication
public class R2DBCApplication{
    public static void main(String... args) {
        SpringApplication.run(R2DBCApplication.class, args);
    }
}

Test.java

@SpringBootTest
@ContextConfiguration(classes = R2DBCConfiguration.class)
public class R2DBCTest {

    @Autowired
    private DatabaseClient client;
    @Autowired
    private UnitR2DBCRepository unitR2DBCRepository;

    @Test
    public void test() {
        ...
    }
}

When I run R2DBCTest, I get the error:

NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.dao.UnitR2DBCRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

question from:https://stackoverflow.com/questions/65916929/why-bean-is-not-found-in-spring-boot-test

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...