Re: Re: Asserts using Comparable.compareTo() instead of equals() (for BigDecimal and others)
Kevin Cooney <[email protected]> Sun, 16 Mar 2014 12:14:08 -0700
| Newsgroups | gmane.comp.java.junit.user |
|---|---|
| Message-ID | <CAA3E+eVafXYAe7UvGV6bCsg4vU6jAsojdAaT2GXL1GhSTJ4aVQ@mail.gmail.com> |
On Sat, Mar 15, 2014 at 11:35 AM, Miguel Muñoz <[email protected]>wrote: > > > Comrades, > > Okay, I checked out the conversations at > https://github.com/junit-team/junit/issues/95 and > https://github.com/junit-team/junit/pull/376, and while I'm not entirely > convinced that hamcrest is the way to go for comparison assertions, I can > certainly live with it. So I began searching the JUnit classes for an > appropriate hamcrest Matcher that would work with BigDecimal values. I > looked in these packages and classes: > > org.junit.matchers.JUnitMatchers > org.junit.internal.matchers > org.hamcrest.CoreMatchers > org.hamcrest > org.hamcrest.core > org.hamcrest.internal > > I couldn't find any classes that helped me compare two BigDecimal values. > I think this was cleared up by the other people on this thread. > Of course, I could search for a Matcher that doesn’t ship with JUnit, or I > could write my own Matcher, but a class like BigDecimal, which shipped with > version 1.0 of the JDK, should be supported by JUnit out of the box. Here’s > why: > > Some of the comments had questions like this: > > What makes you think Comparable assertions are common? > I was the one that asked that question. The question was, admittedly, badly worded, but I also think you might be taking it out of context. The thread is here: https://github.com/junit-team/junit/pull/376#discussion_r418185 In that context, leet3lite was talking about assertions like "isLessThan" or "isGreaterThanOrEqualTo". I was questioning how often those types of assertions would be needed. We couldn't come up with a way to make those kinds of assertions with an API that was easy to use correctly and hard to use incorrectly without creating a mini DSL. Since Hamcrest had a DSL that supported it, we decided not to go forward with changes to JUnit If we only need assertions that use Comparable to check if two objects are equal, that would be easier, and perhaps we should do that. > > > I'm going to guess here that the questioner does not work in financial > services. > Actually, I have worked on billing systems in the past, and I'm my current work includes producing financial reports. In my current project, we do use BigDecimal, and assertEquals() has met our needs. We just use expected values that have the same scale as the value we expect. Perhaps that makes our tests unnecessarily strict and there fragile, but it has worked for us. Although, given how rarely I used it before I joined this industry, I can > certainly understand why non-financial developers would think of BigDecimal > as an obscure class that doesn't get used much. But they're wrong. > I don't think anyone on the thread suggested that BigDecimal was obscure. -- Kevin