"Untestable" code - JtestR

This is a part of "Untestable code" series. See the introduction to know what is it all about (yes, you really should go there, do it).

The main idea of the series is to write unit-tests for a particularly nasty piece of code. In this part I will use JtestR library to write test cases... well, not exactly. The point is that you can't do it with JtestR. :(

After long time and numerous attempts to make JtestR work the way I want it to, it occurred that it's simply not possible. From the creator and maintainer of JtestR - Ola Bini - I've learned that:

no, constructors cannot be mocked. That's simply an
artifact of the way Java works, so this functionality will not make it in.

So, my attempt to use JtestR for testing the "untestable code" failed miserably. :( I was misguided by mocha powerful features - I expected JtestR to take full advantage of them on the Java side, but sadly it is not so.

Anyway, I'm really glad I took some time to get more familiar with JtestR. It's a really nice tool, and I see a bright future for it - I believe, that for writing unit tests you DON'T need full java stack with all the bells and whistles it provides - scripting languages like Ruby are good enough for this job.

So, I'll be definitely back to JtestR !

JtestR artifacts

The latest JtestR jar (version 0.3) is available on mvnrepository.com ...but it won't work unless you download jruby-complete-r6947 from http://dist.codehaus.org/jtestr/ and install it in your local repo.


  • JtestR homepage
  • JtestR mock howto
  • Mocha homepage
  • Mocha - Mock object injection
  • Mockin in Java using Mocha
    This used to be my blog. I moved to http://tomek.kaczanowscy.pl long time ago.


    Please comment using