r2dbc 单元测试 - 存储库在保存实体后没有 return 任何内容

r2dbc Unit Test - repository doesn't return anything after saving entity

我正在使用 R2DBC MySQL 学习 Reative jdbc。我有一个这样的存储库:

public interface UserRepository extends ReactiveCrudRepository<User, Long> {

    Mono<User> findByEmail(String email);

我创建了一个单元测试,以便使用 H2 使用内存数据库测试存储库。测试文件如下所示:

@RunWith(SpringRunner.class)
@DataR2dbcTest
@TestPropertySource(locations = "classpath:application-test.properties")
public class UserRepositoryTest {

    @Autowired
    private UserRepository userRepository;

    @BeforeClass
    public static void initDataBase() {
        ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:h2:mem:///testdb;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;");
        DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
        R2dbcEntityTemplate template = new R2dbcEntityTemplate(databaseClient);

        String query = "create table user (id int unsigned not null AUTO_INCREMENT, email varchar(50) not null, password varchar(100) not null, first_name varchar(50) not null, last_name varchar(50) not null, description varchar(50), phone_number varchar(50), user_image_id int unsigned, need_refresh_pass boolean not null, PRIMARY KEY (id));";
        template.getDatabaseClient().execute(query).fetch().rowsUpdated().block();
    }

该方法会创建 table,因为 R2DBC 不会自动创建它。 然后我有测试:

@Test
public void whenFindingAnUserReturnTheUserIfExist() {
    User user = new User("email@mail.com", "pass", "name", "lastNmae", "description", "123123");
    userRepository.save(user);
    StepVerifier.create(userRepository.findByEmail("email@mail.com")).expectNextMatches(userCreated -> userCreated.getEmail().equals("email@mail.com")).verifyComplete();
}

当我运行测试时,控制台显示:

java.lang.AssertionError: expectation "expectNextMatches" failed (expected: onNext(); actual: onComplete())

findByEmail 方法似乎没有 return 任何东西。 我做错了什么?

POM 文件:

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-r2dbc</artifactId>
        </dependency>

        <!-- For testing possibility -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>dev.miku</groupId>
            <artifactId>r2dbc-mysql</artifactId>
            <version>0.8.2.RELEASE</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-h2</artifactId>
        </dependency>

我意识到我的错误。 我忘了将 .subscribe() 添加到方法 userRepository.save(user);

所以测试方法是:

@Test
public void whenFindingAnUserReturnTheUserIfExist() {
    User user = new User("email@mail.com", "pass", "name", "lastNmae", "description", "123123");
    userRepository.save(user).subscribe();
    StepVerifier.create(userRepository.findByEmail("email@mail.com")).expectNextMatches(userCreated -> userCreated.getEmail().equals("email@mail.com")).verifyComplete();
}