gpt4 book ai didi

java - 在 JUnit 测试(java)中如何使用 Assertj 库检查数组的数组?

转载 作者:行者123 更新时间:2023-12-02 11:11:00 25 4
gpt4 key购买 nike

我想使用assertj来检查数组中的数据是否包含其他数组。

accounts: [class AccountResource {
resourceId: 010001781LV
bicFi: null
accountId: null
name: Livret A Rosalie
details: LIVRET A
linkedAccount: null
usage: PRIV
cashAccountType: null
product: LVA
currency: EUR
balances: [class BalanceResource {
name: null
balanceAmount: class AmountType {
currency: EUR
amount: 10000
}
balanceType: CLBD
lastChangeDateTime: null
referenceDate: 2018-05-31
lastCommittedTransaction: null
}, class BalanceResource {
name: null
balanceAmount: class AmountType {
currency: EUR
amount: 10000
}
balanceType: XPCD
lastChangeDateTime: null
referenceDate: 2018-05-31
lastCommittedTransaction: null
}]
psuStatus: Account Holder
links: null
}

我的 2 个第一个测试用例没问题。我过滤“resourceId=010001781LV”并检查 account.currency=EUR。我过滤“resourceId=010001781LV”并检查 account.balances.size()=2。

assertThat(halAccounts.getAccounts())
.filteredOn(account -> account.getResourceId().equals("010001781LV"))
.extracting(account -> account.getCurrency())
.containsExactly("EUR");
assertThat(halAccounts.getAccounts())
.filteredOn(account -> account.getResourceId().equals("010001781LV"))
.extracting(account -> account.getBalances().size())
.containsExactly(2);

但我想过滤“resourceId=010001781LV”并过滤“balances(foreach).balanceType=CLBD”并检查balanceAmount=10000。

我在其他 lambda 中尝试 lambda 但我需要一些帮助:

assertThat(halAccounts.getAccounts())
.filteredOn(account -> account.getResourceId().equals("010001781LV"))
.filteredOn(account -> account.getBalances().forEach(balance -> {
balance.getBalanceAmount().getAmount().equals("10000");
}))
.extracting(balance -> balance.getBalanceAmount().getAmount())
.contains("1000");

我在第二个 filteredOn 时遇到此错误:

Multiple markers at this line
- The target type of this expression must be a functional interface
- The method filteredOn(Condition<? super AccountResource>) in the type AbstractIterableAssert<ListAssert<AccountResource>,List<?
extends AccountResource>,AccountResource,ObjectAssert<AccountResource>> is not applicable for the arguments ((<no type> account) -> {})

最佳答案

要点是 filterOn 内的表达式应返回 boolean,而 forEach 返回 void

帐户 ID 应为“DEF”,所有“A”余额应为“10”。

@Test
void test() {
Account wrongAcc = new Account("ABC", Collections.emptyList());
Account goodAcc = new Account("DEF", Arrays.asList(
new Balance("A", "10"),
new Balance("A", "10"),
new Balance("B", "20")
));
Account wrongBalanceAcc = new Account("DEF", Arrays.asList(
new Balance("A", "10"),
new Balance("A", "20"),
new Balance("B", "20")
));

List<Account> accountList = Arrays.asList(wrongAcc, goodAcc, wrongBalanceAcc);

assertThat(accountList)
.filteredOn(acc -> acc.getId().equals("DEF"))
.filteredOn(acc ->
acc.getBalances()
.stream()
.noneMatch(balance -> balance.getType().equals("A") && !balance.getAmount().equals("10"))
).containsExactly(goodAcc);
}

关于java - 在 JUnit 测试(java)中如何使用 Assertj 库检查数组的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50622037/

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