如何将 Reactive Java StepVerifier 应用于 Pojo

How do I apply a Reactive Java StepVerifier to a Pojo

我有以下代码(请参阅下文),它成功地使用 StepVerifier 调用 ParsePerson::Parse 方法,然后为“John”和“Mary”调用 expectNext。

如果 ParsePerson::Parse 方法返回一个 Person(而不是 String),我将如何更改 StepVerifier 以检查每个 Person 的属性。与“testPerson”相同的方式

package com.chocksaway;

import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

/**
 * Author milesd on 24/04/2022.
 */
public class TestFluxFilterMono {

    @Test
    public void testPerson() {
        Person john = new Person("John", 22);
        StepVerifier.create(Flux.just(john.getName(), john.getAge()))
                    .expectNext("John", 22)
                    .expectComplete()
                    .verify();
    }

    @Test
    public void testCallParse() {
        StepVerifier.create(ParsePerson.parse())
                .expectNext("John")    // how would I check for a Person "John", 22?
                .expectNext("Mary")    // how would I check for a Person "Mary", 33?
                .verifyComplete();
    }
}

class ParsePerson {
    static Flux<String> parse() {
        Flux<Person> peopleList = Flux.just(new Person("John", 22), new Person("Mary", 33));

        return peopleList
                .filter(each -> each.getAge() > 20)
                .map(Person::getName);
    }
}

class Person {
    private final String name;
    private final int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }
}

解决方案 1

  1. 根据 agename 字段为 Person class 定义 equalshashCode :
class Person {

    private final String name;
    private final int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        final Person person = (Person) o;
        return getAge() == person.getAge() && Objects.equals(getName(), person.getName());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getName(), getAge());
    }
}
  1. 修改parse方法:
static Flux<Person> parse() {
    Flux<Person> peopleList = Flux.just(new Person("John", 22), new Person("Mary", 33));
    return peopleList
            .filter(each -> each.getAge() > 20);
}
  1. 测试一下:
@Test
public void testCallParse2() {
    StepVerifier.create(ParsePerson.parse())
            .expectNext(new Person("John", 22)) 
            .expectNext(new Person("Mary", 33))
            .verifyComplete();
}

解决方案 2

使用 consumeNextWith 并在那里断言字段:

@Test
public void testCallParse2() {
    StepVerifier.create(ParsePerson.parse())
            .consumeNextWith(person -> {
                assertEquals("John", person.getName());
                assertEquals(22, person.getAge());
            })
            .consumeNextWith(person -> {
                assertEquals("Mary", person.getName());
                assertEquals(33, person.getAge());
            })
            .verifyComplete();
}