Monthly Archives: February 2013

Mockito and floating point objects like BigDecimal

Last week I saw a Java project which used mockito. Great! Only point of attention was that it used a BigDecimal object as a key / identifier. So:

when(new BigDecimal(11)).thenReturn("test 1");

This runs fine when the BigDecimal does not have a scale (like in the example). It is different story when a scale is added, for example a currency amount.

when(new BigDecimal(11.78)).thenReturn("test 1");

This uses the default matcher, which then will compare (on the equals()) like:

 11.17999999999992888474774 to 11.78

Ergo; the above when with 11.78 will never return a value. To fix this you can use your own matcher. Use the following code for this:

when(argThat(new CurrencyMatcher(new BigDecimal(11.78))).thenReturn("test 1");

And the CurrencyMatcher class will then be as followed.

class CurrencyMatcher extends ArgumentMatcher {
      BigDecimal bigDecimal;

      public CurrencyMatcher(BigDecimal bigDecimal) {
          this.bigDecimal = bigDecimal;
      }

      public boolean matches(Object bigDecimalB) {
          if(bigDecimalB == null && bigDecimal != null) {
              return false;
          }

          if(bigDecimalB == null && bigDecimal == null) {
              return true;
          }

          NumberFormat eurCostFormat = NumberFormat.getCurrencyInstance(Locale.NL);
          return eurCostFormat.format(bigDecimal).equals(eurCostFormat.format(bigDecimalB);
      }
NTFS-3G-Wait-Error

TrueCrypt sharing files Mac and Ubuntu

When using TrueCrypt on your Mac you might want to backup or synchronize your TrueCrypt volumes to a non-Mac formatted (like NTFS ) external (USB) harddisk so you can view or edit the volumes under Linux  /Ubuntu or Windows.

By default, Mac OS X can only read NTFS formatted volumes, not write to it. To enable this, you have to install NTFS-3G for Mac OS X 2010.10.2

On some Mac OS X version (Lion in my case) you will additionally add a patch to prevent the following error message. This patch can be downloaded here

NTFS-3G-Wait-Error