mockito.when()在模拟假客户机时不在单元测试中工作

vjrehmav  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(177)

我有一个组件类,如下所示:

@Component
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
public class OutboundSystemTriggeredDirectAnalyzer implements BufferAccountAnalyzer {

    private FeignCustomerClient customerClient;

    @Override
    public void analyze(BankStatementTransaction bankStatementTransaction) {
        CustomerSummaryResponseDto customerSummary = new CustomerSummaryResponseDto();
        CustomerSummaryBankAccountRequestDto parsedAccountNumber = CustomerUtils
                .parseAccountNumber(bankStatementTransaction.getAccountNumber());
        try {
            customerSummary = customerClient
                    .getCustomerSummaryByBankAccount(parsedAccountNumber).getBody();
        } catch (Exception e) {
            log.error("Error on getting customer summary for bank account: {}", parsedAccountNumber);
        }

        CustomerIdentity customerIdentity = null;
        if (customerSummary != null && customerSummary.getProducts() != null) {
            customerIdentity = CustomerUtils.identifyCustomer(customerSummary);
        }
}

外国客户是

@FeignClient(CUSTOMER_MANAGEMENT)
public interface FeignCustomerClient {

    @PostMapping(CUSTOMERS_PATH + SUMMARY + READ + BY_BANK_ACCOUNT)
    ResponseEntity<CustomerSummaryResponseDto> getCustomerSummaryByBankAccount(CustomerSummaryBankAccountRequestDto request);

}

我做了个测试

@RunWith(MockitoJUnitRunner.class)
public class OutboundSystemTriggeredDirectAnalyzerTest {

    @Mock
    private FeignCustomerClient customerClient;
    @Mock
    private FeignOldtransactionClient transactionClient;
    @InjectMocks
    private OutboundSystemTriggeredDirectAnalyzer analyzer;

    @Test
    public void matchCustomer() {
        BankStatementTransaction bankStatementTransaction = new BankStatementTransaction();
        bankStatementTransaction.setAccountNumber("31-046-006770124");
        CustomerSummaryBankAccountRequestDto parsedAccountNumber = new CustomerSummaryBankAccountRequestDto(31, 46, "6770124");

        CustomerSummaryResponseDto customerSummary = new CustomerSummaryResponseDto();
        BaseProductDto baseProductDto = new BaseProductDto(ProductType.LENDER, ProductSubType.PRIVATE, 32L);
        List<BaseProductDto> baseProducts = new ArrayList<>();
        baseProducts.add(baseProductDto);
        customerSummary.setProducts(baseProducts);

        WithdrawSearchRequestDto withdrawSearchRequestDto = new WithdrawSearchRequestDto();
        withdrawSearchRequestDto.setIsApproved(TRUE);
        withdrawSearchRequestDto.setLenderId(32L);

        TransactionResponseDto transactionDto = new TransactionResponseDto();
        transactionDto.setAmount(new BigDecimal("2000.00"));
        List<TransactionResponseDto> transactions = new ArrayList<>();
        transactions.add(transactionDto);

        Mockito.when(customerClient.getCustomerSummaryByBankAccount(parsedAccountNumber)).thenReturn(ResponseEntity.ok(customerSummary));
        Mockito.when(transactionClient.searchLenderWithdrawTransactions(1L, withdrawSearchRequestDto)).thenReturn(ResponseEntity.ok(transactions));

        analyzer.analyze(bankStatementTransaction);

        assertThat(bankStatementTransaction.getBankTransactionType().getId(), is(9L));

    }

}

当调用customerclient.getcustomersummarybybankaccount以返回我在测试中创建的customersummary对象时,我希望这样做,但当我调试时,它不会返回customersummary,但它为null。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题