首页 > 解决方案 > 测试响应式存储库 java.lang.AssertionError:期望“assertNext”失败(期望:onNext();实际:onComplete())

问题描述

我正在编写一个简单的反应式应用程序,使用 spring5 和 mongo 反应式存储库。我想测试回购,遵循教程,但仍然有问题:

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

这是我的实体:

@Document(collection = "products")
@TypeAlias("product")
@Getter
@Builder
@AllArgsConstructor
@EqualsAndHashCode
public class Product {

    @Id
    private ObjectId _id;
    private String productName;
    private Integer quantityPerUnit;
    private BigDecimal unitPrice;
    private Integer unitsInStock;
    private Boolean discount;
    private String categoryName;

    private Supplier supplier;

}

回购,没什么特别的:

public interface ProductRepository extends ReactiveMongoRepository<Product, String> {
}

最后测试:

@DataMongoTest
@RunWith(SpringRunner.class)
public class ProductRepositoryTest {

    @Autowired
    private ProductRepository productRepository;
    @Test
    public void shouldReturnOneProductWithExpected() {
        String expectedId = "54759ab3c090d83494e2d804";
        productRepository.save(first).block();
        Mono<Product> product = productRepository.findById(Mono.just("54759ab3c090d83494e2d804"));

        StepVerifier
                .create(product)
                .assertNext(prod -> {
                    assertNotNull(prod);
                    assertThat(prod.get_id(), is(equalTo(expectedId)));
                })
                .expectComplete()
                .verify();
    }


}

有谁知道为什么它不起作用?

标签: javaspringmongodbreactive

解决方案


我有一个类似的问题,这个对我有用:

    @Test
    fun `should go to end-point and retrieve id`() {
        val generateUniqueId: Mono<String> = idGenerationRepository.generateUniqueId()
        StepVerifier
            .create(generateUniqueId)
            .expectNextMatches { it.isNotEmpty() }
            .verifyComplete()
    }

不确定,但似乎关于verifyX, assertX,的 API 有点令人困惑expectX


推荐阅读