Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What currency to use in unit tests?

I'm developing an application that relies heavily on Joda-Money, and have a number of unit tests that verify my business logic. One (admittedly minor) sticking point for me has been what sort of Money/BigMoney objects to test with; specifically, what CurrencyUnit to use.

As I see it, I have a couple of options:

  • Just use USD

    This is clearly the easiest way to go, and most of my actual application will be working with US Dollars so it makes a fair bit of sense. On the other hand, it feels rather US-centric, and I'm concerned it would risk letting currency-specific errors go unchecked.

  • Use another real currency, like CAD

    This would catch erroneous hard-codings of USD, but otherwise isn't much better than just using USD.

  • Use a dedicated "fake" currency, i.e. XTS

    This clearly makes sense, after all, XTS is "reserved for use in testing". But Joda denotes psuedo-currencies as currencies with -1 decimal places. In practice, the primary difference between currencies in Joda-Money is the number of decimal places, so this risks masking any errors involving decimal place precision, such as erroneously rounding to an integer value.

  • Register my own custom currency with CurrencyUnit.registerCurrency()

    This would obviously work, but seems a little odd seeing as there are alternatives.

  • Use a CurrencyUnit instance created by a mocking library

    Pretty much the same as registering a custom currency.

Again, this is obviously a minor issue, but I'm curious if there's a standard practice for cases like this, or if there's a clear reason to prefer one of these options in particular.

like image 564
dimo414 Avatar asked Mar 12 '15 02:03

dimo414


1 Answers

Use USD (or, generally, whatever currency is most commonly used in your application). I say this for two reasons:

  • Good test data is unremarkable in every respect except that part of it which the test is actually about. When you're writing tests that don't have anything to do with differences between currencies, you don't want to have to think about differences between currencies. Just use whatever is most natural in the application.

  • The idea that using an unusual currency everywhere will somehow result in better testing of unusual currencies is a red herring. Tests should be explicit and focused. If you need to test something about a specific currency, write a test whose point is to test that thing. And if a test is not about a specific currency, it shouldn't break when handling of some unusual aspect of that currency breaks -- it's not valuable to have half your tests break for the same reason; you only want one to break. So there is just no need to spread unusual currencies around the test suite and hope that that will catch something. Instead, optimize for readability; see point 1.

like image 74
Dave Schweisguth Avatar answered Sep 22 '22 10:09

Dave Schweisguth