【发布时间】:2022-01-25 19:26:19
【问题描述】:
首先执行save() 方法,该方法通过测试,直到达到一个条件,如果为真则调用saveBankAccountAndRole() 方法,如果为假则发送Mono.error(new Exception("...").
sizeAccounts(String customerId) 方法确实通过了测试。
saveBankAccountAndRole(BankAccountDto bnkDto)方法中,执行sizeAccounts()方法后,测试不通过,我漏了什么?
public Flux<BankAccountDto> findAccountsByCustomerId(String customerId) {
return mongoRepository
.findAll()
.filter(ba ->
ba.getCustomerId().equals(customerId))
.map(AppUtils::entityToDto);
}
private Mono<Long> sizeAccounts(String customerId){
return findAccountsByCustomerId(customerId)
.count();
}
private Mono<BankAccountDto> saveBankAccountAndRole(BankAccountDto bnkDto) {
return sizeAccounts(bnkDto.getCustomerId())
.flatMap(number -> {
bnkDto.setOpeningOrder(number + 1);
return mongoRepository.save(AppUtils.dtoToEntity(bnkDto))
.switchIfEmpty(Mono.error(new Exception("Problem saving the bank account")))
.zipWhen(bnk -> {
var customerRoleDto = new CustomerRoleDto();
customerRoleDto.setBankAccountId(bnk.getBankAccountId());
customerRoleDto.setCustomerId(bnkDto.getCustomerId());
customerRoleDto.setRoleType(bnkDto.getRoleType());
return webClientRoleHelper.saveCustomerRole(customerRoleDto);
})
.switchIfEmpty(Mono.error(new Exception("Problem saving roles")))
.map(tuple -> AppUtils.entityToDto(tuple.getT1()));
});
}
测试:
@Mock
private IMongoBankAccountRepository mongoRepository;
@InjectMocks
private BankAccountServiceImpl bankAccountServiceImpl;
@Test
void saveBankAccountAndRoleTest() throws Exception {
when(mongoRepository.findAll()).thenReturn(Flux.just(bnkDto)
.map(AppUtils::dtoToEntity));
when(mongoRepository.findAll().filter(ba ->
ba.getCustomerId().equals(customerId)))
.thenReturn(Flux.just(bnkDto).map(AppUtils::dtoToEntity));
StepVerifier.create(bankAccountServiceImpl.findAccountsByCustomerId(customerId))
.expectSubscription()
.expectNext(bnkDto)
.verifyComplete();
var spy = PowerMockito.spy(bankAccountServiceImpl);
PowerMockito.when(spy, "sizeAccounts", customerId)
.thenReturn(Mono.just(2L));
PowerMockito.when(spy, "saveBankAccountAndRole",bnkDto)
.thenReturn(Mono.just(bnkDto));
}
例外:
java.lang.AssertionError:预期“expectNext(com.nttdata.bootcamp.model.dto.BankAccountDto@147c4523)”失败(预期值:com.nttdata.bootcamp.model.dto.BankAccountDto@147c4523;实际值: com.nttdata.bootcamp.model.dto.BankAccountDto@551725e4) 在 com.nttdata.bootcamp.business.impl.BankAccountServiceImplTest.saveBankAccountAndRoleTest(BankAccountServiceImplTest.java:267)
verifyComplete() 时发送给我
【问题讨论】:
-
你在
BankAccountDto类中实现了toString和equals吗? -
不,但我只是放置了那些 lombok 注释并重新测试,它引发了另一个异常:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue: MonoJust cannot be returned by findAll() findAll() should return Flux。这导致了saveBankAccountAndRole的 PowerMockito
标签: spring mockito spring-webflux powermockito