How to test using assertEquals an element of a Set in Java?

Viewed 239

I need to make unit testing for a Java method, and I have a Set like this:

Set<CreditCard> creditCard = ...

And I need to test if an element of this Set is equal with a specific value. It is very easy to do that if creditCard is an ArrayList, I can do something like this:

assertEquals(creditCard.get(0).getCreditCardBalance(), CREDIT_CARD_BALANCE);

But unfortunately this is a Set and not an ArrayList. How can I test this? Thank you!

1 Answers

Yuu can do something like this:

assertTrue(creditCardSet.stream().anyMatch(cc -> cc.getCreditCardBalance() == CREDIT_CARD_BALANCE))
Related