Details
-
Type:
New Feature
-
Status:
Resolved
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: FEST-Assert 1.1
-
Fix Version/s: FEST-Assert 2.0M1
-
Component/s: Assert
-
Labels:None
Description
Sometimes you can be interested in comparing objects using a custom comparator instead of using the default equals() method definitions.
For example, let's say you have a list of invoices and want to make sure there isn't more than one invoice with the same date.
You can't use the doesNotHaveDuplicates() method since that uses the equals() method on the objects.
The solution is to specify the comparator to use and then makes assertions (the comparator being used for all chained assertions).
assertThat(firstInvoiceList).usingComparator(DATE_COMPARATOR).doesNotHaveDuplicates();
where DATE_COMPARATOR compares the two invoices just by the date on each.
assertThat(firstInvoiceList).usingComparator(PAYEE_COMPARATOR).isEqualTo(secondInvoiceList).contains(someInvoice);
that would make sure that the two lists are equal and it contains someInvoice, but only paying attention to the Payee property for each.
The assertions based a custom comparator only last for the chained assertions, if you write :
assertThat(firstInvoiceList).usingComparator(PAYEE_COMPARATOR).isEqualTo(secondInvoiceList).contains(someInvoice); assertThat(firstInvoiceList).isEqualTo(secondInvoiceList);
then the second line does not use a PAYEE_COMPARATOR comparison strategy (by default new assertions use equals() comparison strategy).
— initial request From Ted Young: ----
Sometimes I'm interested in comparing objects using a custom comparator instead of using the default equals() method definitions. For example, I have a list of invoices and I want to make sure there isn't more than one invoice with the same date.
I can't use the doesNotHaveDuplicates() method since that uses the equals() method on the objects. I'd like to use something like
assertThat(firstInvoiceList).doesNotHaveDuplicates(DATE_COMPARATOR);
where DATE_COMPARATOR compares the two invoices just by the date on each.
This would also apply to methods such as isEqualTo(), so that I can say:
assertThat(firstInvoiceList).isEqualTo(PAYEE_COMPARATOR, secondInvoiceList);
that would make sure that the two lists are equal, but only paying attention to the Payee property for each. An alternative would be:
assertThat(firstInvoiceList).using(PAYEE_COMPARATOR)
.isEqualTo(secondInvoiceList);
But that would make it difficult to combine my prior duplicate assert with this one, e.g.:
assertThat(firstInvoiceList).doesNotHaveDuplicates(DATE_COMPARATOR)
.isEqualTo(PAYEE_COMPARATOR, secondInvoiceList);
Original report: Issue 148 (Google Code)
Issue Links
- relates to
-
FEST-252
ignoringCase() feature for String assert
-
As much as I think this is a very useful feature, I'm unable to implement it due to generics. Comparator uses generics for type safety, by "generifying" the type of objects a comparator can take. The problem is that ObjectAssert (for example) does not use generics. It works at the Object level. I don't see the point of adding generics to ObjectAssert either. Please see the attached context (Mylyn) to see the compilation error I got. Since I don't see a viable solution for this issue, I'm marking this issue as "Won't Fix".