gpt4 book ai didi

java - 测试响应式(Reactive)存储库 java.lang.AssertionError : expectation "assertNext" failed (expected: onNext(); actual: onComplete())

转载 作者:行者123 更新时间:2023-12-03 19:16:28 29 4
gpt4 key购买 nike

我正在编写一个简单的响应式(Reactive)应用程序,使用 spring5 和 mongo 响应式(Reactive)存储库。我想测试 repo ,跟着教程,但仍然有问题:

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;

}

repo ,没什么特别的:
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();
}


}

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

最佳答案

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

    @Test
fun `should go to end-point and retrieve id`() {
val generateUniqueId: Mono<String> = idGenerationRepository.generateUniqueId()
StepVerifier
.create(generateUniqueId)
.expectNextMatches { it.isNotEmpty() }
.verifyComplete()
}
不确定,但似乎关于 verifyX 的 API 有点困惑, assertX , expectX

关于java - 测试响应式(Reactive)存储库 java.lang.AssertionError : expectation "assertNext" failed (expected: onNext(); actual: onComplete()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60318198/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com