java - 测试响应式存储库 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();
}
}
有谁知道为什么它不起作用?
解决方案
我有一个类似的问题,这个对我有用:
@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
推荐阅读
- r-mosaic - 分布图中的两条尾巴
- reactjs - 反应删除多个项目
- c# - C# - 将'foreach'中的变量传递给其他文件
- react-native - 在 React Native Redux 中获取 store not found 错误
- c++ - 在循环中显示不同值的初始化数组
- circleci-2.0 - CircleCI 迁移:配置文件
- iphone - 使用 iPhone Telnet 没有响应
- string - gcc 中的 STM32 和常量字符串
- sql-server - 使用 VBA 记录集将 Excel 工作表导出到 SQL Server 表
- sql-server - 带有 MSSQL Express 的 Power BI 服务器