在JUnit test(java)中如何使用Assertj librairy检入数组数组?

时间:2018-05-31 10:22:46

标签: java unit-testing lambda junit assertj

我想使用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'过滤'余额(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) -> {})

2 个答案:

答案 0 :(得分:1)

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

帐户ID应为&#34; DEF&#34;和所有&#34; A&#34;余额应该有&#34; 10&#34;值。

@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);
}

答案 1 :(得分:0)

如果我是正确的,以下代码不会返回布尔值(forEach不会):

account.getBalances().forEach(balance -> {
    balance.getBalanceAmount().getAmount().equals("10000");
})

表示它不是

中使用的有效Predicate
filteredOn(account -> account.getBalances().forEach(balance -> {
    balance.getBalanceAmount().getAmount().equals("10000");
}))

我相信java会尝试应用filteredOn(Condition)这是不可能的。

修改

如果您的问题是如何过滤帐户havinn 所有他们的余额等于10000?那么你可以使用这个Predicate(未经测试,但应该足够接近你想要的):

account -> account.getBalances().stream()
                  .map(balance ->  balance.getBalanceAmount().getAmount())
                  .allMatch(amount -> amount.equals("10000"))

请参阅https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#allMatch-java.util.function.Predicate-