简体   繁体   中英

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

I'm learning Reative jdbc with R2DBC MySQL. I have a repository like this:

public interface UserRepository extends ReactiveCrudRepository<User, Long> {

    Mono<User> findByEmail(String email);

I created an unit test in order to test the repository with a memory data base usin H2. The test file looks like this:

@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();
    }

That method creates the table because R2DBC doesn't create it automatically. And then I have the test:

@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();
}

When I run the test, the console shows:

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

It seems to be that the findByEmail method doesn't return anything. What am I doing wrong?

POM file:

   <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>

I realized my mistake. I forgot to add .subscribe() to the method userRepository.save(user);

So the test method is:

@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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM