无法使用spring boot为r2dbc运行测试

vdgimpew  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(515)

我跟随这个链接用spring数据实现r2dbc。该项目有巨大的pom,因为它演示了其他功能。所以我试着只依赖spring引导和r2dbc与h2。
我的pom是:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!--For testing-->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.199</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- junit 5 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>

        <!--For reactive-->
        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-h2</artifactId>
            <version>0.8.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-r2dbc</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
        </dependency>
        <!---->
<dependencies>

我将存储库定义为:

public interface ReactiveFeatureRepository extends ReactiveCrudRepository<Feature, UUID> {
}

要素是实体类。
我在src/test/resources的application.properties中配置了h2:

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa

试验中:

@ExtendWith(SpringExtension.class)
@SpringBootTest
public class ReactiveFeatureRepositoryTest {

    @Autowired
    ReactiveFeatureRepository rfr;

    @Autowired
    DatabaseClient client;

    @Autowired
    H2ConnectionFactory factory;
...
...
}

但是当我尝试运行测试时,我得到了大量的日志,其中列出了“积极匹配”和“消极匹配”,最后:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 
'x.y.z.ReactiveFeatureRepositoryTest': Unsatisfied dependency expressed 
through field 'rfr'; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 
'x.y.z.ReactiveFeatureRepository' available: expected at least 1 bean which 
qualifies as autowire candidate. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at

与示例项目相比,我缺少什么?

quhf5bfb

quhf5bfb1#

对于spring数据r2dbc,db connection config前缀是spring.r2dbc。
看看我的例子
如果类路径中有h2 r2dbc驱动程序,则可以忽略h2/r2dbc配置。
这里有一个测试h2存储库的示例。
有关其他数据库支持和使用testcontainers编写测试代码的信息,请查看此存储库中的其他r2dbc示例。
最新的r2dbc有一个新的存储库,它演示了即将到来的springboot2.4/springdatar2dbc/spring5.3r2dbc的特性。

相关问题