【发布时间】:2020-09-10 19:54:46
【问题描述】:
我在编写一些集成测试时遇到了一些困难。我有类似的东西
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Service.class)
public class ServiceTest {
@Rule
public PostgreSQLContainer postgres = new PostgreSQLContainer();
@Autowired
Service Service;
@Before
public void setUp() {
PostgresqlConnectionFactory connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
.host(postgres.getHost())
.port(postgres.getFirstMappedPort()) // optional, defaults to 5432
.username(postgres.getUsername())
.password(postgres.getPassword())
.database(postgres.getDatabaseName()) // optional
.build());
Resource resource = new ClassPathResource("sql.sql");
Mono<PostgresqlConnection> mono = connectionFactory.create();
mono.map(connection -> connection
.createStatement(Helpers.asString(resource))
.execute()).block();
}
@Test
public void test() {
Request Request = new Request();
request.setName("name");
Mono<Item> itemMono = Service.createNewHub(hubRequest);
Item item = itemMono.block();
Assert.assertEquals(1L, 1L);
}
}
我的 Service.class 如下所示
@Service
public class Service {
private Repository repository;
public Service(Repository repository) {
this.repository = repository;
}
public Flux<Item> getAllItems() {
return repository.findAll();
}
}
还有我的回购
@Repository
public interface Repository extends ReactiveCrudRepository<Item, Integer> {
}
我的错误如下
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.Repository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
虽然我在应用程序中编写的所有代码都可以正常注入,但对于 ReactiveCrudRepos,我在获取它们的实例化对象方面没有任何运气。我需要做什么才能创建和注入实现?
【问题讨论】:
标签: spring-boot spring-webflux spring-test