An Architect's View

CFML, Clojure, Software Design, Frameworks and more...

An Architect's View

CFEasyMock on RIAForge

March 31, 2008 · 6 Comments

As a fan of unit testing, I was interested to hear about a new mock object generator project appearing in the CF world. I've been using Brian Kotek's ColdMock for a while and I really like how easy it makes it to test CFCs that depend on several other CFCs - because you can create "mock" versions of those CFCs on the fly that return specific values. Mock objects are a good way to provide a consistent environment for your CFCs under test, as well as a way to let your CFCs be tested without them affected the "real" environment (because you can create a "mock" environment which can even include things such as data access objects to fake the whole database layer). As I blogged recently, I've switched from cfcUnit to MXUnit and the mailing list is pretty active. Mike Steele posted that he had ported EasyMock to CFML. My first reaction was "Have you looked at Brian Kotek's ColdMock project?" and Mike explained that EasyMock isn't just about creating simple mock objects but about verifying behavior in those mock objects. Intrigued, I read about EasyMock, a Java project that targets JUnit, and realized the power of being able to create mock objects that expect to be called in certain ways and can, as part of your unit test, actually verify the calls made on them! I asked Mike to send me a build and I played with it and liked it a lot, pretty much instantly. I sent him some feedback and very soon I got an updated build that incorporated much of my feedback - and a note saying he was already going in that direction. Nice to find a project where I'm on the same page as the author! You can download the latest build from the CFEasyMock project page on RIAForge. It's a very simple but very powerful concept that should really help you with your unit testing!

Tags: coldfusion · oss · tdd

6 responses so far ↓

  • 1 Brian LeGros // Apr 1, 2008 at 5:13 AM

    Great post Sean. Glad to finally hear someone in the CF community talking about the uses for mock objects. Mock objects are great tools which lead to good architectural practices and the path to TDD/BDD (if that interests you). With the use of mocks, you can get extremely close to producing regression testable unit test suites. I've never been a fan of explicitly using the replay model when using mocks, but who cares, it's great to finally have more tools available to the CF community that have been staples in other communities for years.

    If anyone is interested, there is an old, but great post from Martin fowler about the differences between mocks and stubs: http://martinfowler.com/articles/mocksArentStubs.html
    ColdMock is actually just creating stubs rather than mock objects which are helpful but not the solution when you are trying emulate behavior. I've actually been working on a library which is more like Mocha from the Ruby world, but building DSLs in ColdFusion is no easy task (especially for a lazy, I mean slow paced, guy like myself). :)

    Great post.
  • 2 matias elgart // Apr 1, 2008 at 10:56 AM

    @Brian:

    i'm not sure if i'd agree that ColdMock is simply about making Stubs. When you use ColdMock, you can set up your expectations and explicitly declare the number of times a certain method will be called and what it will return. That seems more behavioral to me, whereas Stubs are more simply about returning canned answers. Perhaps ColdMock is somewhere between a purist Mock object and a Stub object. that's what i got out of Fowler's "mocks aren't stubs" article, anyway.

    am i understanding ColdMock and the difference between Stubs and Mocks correctly?
  • 3 Sean Corfield // Apr 1, 2008 at 11:38 AM

    And it's only fair to point out that ColdMock added the ability to declare the number of times a value is returned recently (in response to CFEasyMock, in fact). However, ColdMock does not verify that the stub is called that many times - it only specifies that it returns a given value a certain number of times. It'll fail if you call it more times but there's no way to check it was not called fewer times. In fact I have unit tests where I create a stub with ColdMock where I have it return a fixed value for, say, 5 calls but I may only call it once in some tests.

    Brian's claim that ColdMock only creates stubs is accurate but until CFEasyMock implements "nice" mocks (coming soon), ColdMock is still very useful (and will continue to be useful even after CFEasyMock provides "nice" mocks.

    Both approaches are useful, to be honest, and we must remember that most CFers are not even doing unit testing, let alone using stubs or mocks!
  • 4 Jaime Metcher // Apr 1, 2008 at 2:43 PM

    @Brian

    +1! See http://www.mail-archive.com/cfcdev@googlegroups.com/msg00026.html

    It may seem kind of trivial, but one of the promises of the patterns movement was to promote communication via a common vocabulary. If it's a stub let's call it a stub, and then we can have the discussion about where and whether we actually want to use mocks - there are some good arguments pro and con.

    But I agree that whatever you call it, it's great to have this available for CF.
  • 5 Sean Corfield // Apr 1, 2008 at 6:44 PM

    FWIW, I thought stubs were mocks when I first got started down this path... :)
  • 6 Brian LeGros // Apr 1, 2008 at 7:42 PM

    @Sean - I can definitely agree with you about the effectiveness of stubs as well as mock objects. When I build my unit test suites, I like to stub out my tests that touch external resources (web service, database, etc.) so I can continue coding until I'm ready to focus on testing my interactions with those resources explicitly. Brian Kotek has done a great job solving some of the tedious problems with respect to reflection and mock objects in ColdMock. I guess I should choose my words more carefully however. Using stubs shouldn't have a negative connotation associated with it; it's just a different tool in the toolbox.

Leave a Comment

Leave this field empty: