[英]in JUnit test (java) How to check in a array of array with Assertj librairy?
I want use assertj for cheking a data in array contain a other array. 我想使用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
}
My 2 first tests case are OK. 我的两个第一个测试用例都还可以。 I filter on 'resourceId=010001781LV' and I check account.currency=EUR.
我筛选了“ resourceId = 010001781LV”,然后检查了account.currency = EUR。 I filter on 'resourceId=010001781LV' and I check account.balances.size()=2.
我过滤了'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);
but I want filter on 'resourceId=010001781LV' and filter on 'balances(foreach).balanceType=CLBD' and check balanceAmount=10000. 但是我想在'resourceId = 010001781LV'上过滤并在'balances(foreach).balanceType = CLBD'上过滤并检查balanceAmount = 10000。
I try lambda in other lambda but I need some help : 我在其他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");
I have this error on 2nd filteredOn
: 我在2nd
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) -> {})
The point is that expression inside filterOn
should return boolean
whereas forEach
returns void
. 关键是
filterOn
内的filterOn
应该返回boolean
而forEach
返回void
。
Account id should be "DEF" and all "A" balances should have "10" value. 帐户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);
}
If I am correct the following code does not return a boolean ( forEach does not): 如果我是正确的,则以下代码不会返回布尔值( forEach不会):
account.getBalances().forEach(balance -> {
balance.getBalanceAmount().getAmount().equals("10000");
})
which means that it is not a valid Predicate
to be used in 这意味着它是不是一个有效
Predicate
中使用
filteredOn(account -> account.getBalances().forEach(balance -> {
balance.getBalanceAmount().getAmount().equals("10000");
}))
I believe java tries to apply filteredOn(Condition)
which is not possible. 我相信java尝试应用
filteredOn(Condition)
,这是不可能的。
Edit 编辑
If your question is how do I filter account havinn all their balances amount equal to 10000 ? 如果您的问题是我该如何过滤帐户havinn 所有等于10000的余额? then you can use this
Predicate
(not tested but should be close enough to what you want): 那么您可以使用以下
Predicate
(未经测试,但应该与所需的Predicate
足够接近):
account -> account.getBalances().stream()
.map(balance -> balance.getBalanceAmount().getAmount())
.allMatch(amount -> amount.equals("10000"))
See https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#allMatch-java.util.function.Predicate- 参见https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#allMatch-java.util.function.Predicate-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.