CITCON Paris 2009: Mock objects

Interfaces

During the session on mock objects there was a digression about interfaces. I have seen too often interfaces in a way that I don’t like. I will use the same example as Eric:

Let’s say that you have a FileManager, providing some services to manage files I suppose ;-) , you may have an interface called IFileManager. And usually there is only one implementation of IFileManager which is FileManager.

I think this is wrong for at least two reasons:

  • Usefulness. If there is only one implementation, why do you need an interface?
  • Naming. The interface name should represent the “role”, so FileManager is suitable for the interface name, IFileManager has no meaning. Then the implementation should reflect what kind of implementation you have, like LocalFileManager, DistributedFileManager or a DummyFileManager for your tests (but not an ugly FileManagerImpl).

So usually, when I see a software with that kind one 1 to 1 relationship between interface and implementation and using bad names, it raises a warning light in my head, telling me that the person who wrote that code did not really now what he was doing (only applying some old and bad coding rules without trying to understand why it was useful for). As Antonio says, prefix ‘I’ for interface and suffix ‘Impl’ for implementations are signs of code smell.

I even have seen some interfaces with only one or two methods, the implementation had a lot more methods… and the concrete class was directly used in other classes… so yes, very useful interface :-/.

Sometimes, when writing tests, I need to mock some classes that I haven’t defined any interface for… and since several mock libraries are able to mock concrete classes I still not extract any interface.

I like simple classes, with simple roles, so mostly all public methods (except constructor and setters) are the “implied” interface.

So my point on interfaces is “use an interface only when you really need it” (that reminds me YAGNI):

  • when you need several implementations of a given “role”,
  • when defining some “ability” (sorry I don’t find the right term) like Clonable, Closable, Comprable, Serializable, Anything-able (if you can add “able” at the end, it’s a good sign that you might be able to extract an interface for that ;-) ).
Mock objects

So yes, we also spoke about mock objects. Steve Freeman was trying to explain us some stuff, I have the feeling that there was something in his speech that was enlightening but I didn’t really get it (that’s why it’s only a feeling for the moment).

What I remember is that, when writing tests:

  • mock the collaborating classes that change the outside world,
  • use stubs, dummy implementations, etc. otherwise.

I don’t fully understand the reason yet. But something I learn recently and that was says during the session: mock only the code you own, don’t mock external resources.

So for instance, if you have a Customer object, a table full of customers in your database, don’t try to mock JDBC classes like Connection, ResultSet and so on. Create a class accessing the data, let say CustomerDAO (I don’t like the name, but hey, it’s only an example), and then you can mock your CustomerDAO in your software.

I imagine that CustomerDAO will then be tested in integration tests (it’s a class using external software/server/stuff right? Can’t really unit test it (except maybe some data storage specific logic I may have to write in it)).

Anyway, it was an interesting session.

Misc

Books recommended during the session:

Frameworks:

  • jMock (the framework I usually use)
  • EasyMock
  • Mockito (more recent, I started using it a bit at work a week ago, looks quite nice)

CITCON Paris 2009: Goodies

This year I went to CITCON Europe 2009 in Paris, a conference about continuous integration, held at ISEP. It was a quite interesting event. Since everybody as already blogged about it, I will start with a subject I think was not yet covered: the goodies.

As I was talking about it before, I love goodies :-/. Like any conference, I got a bag with some goodies. I apologize for the crappy pictures.

The bag containing all the goodies was in plastic… I liked the recycled paper bags used at XP Day France 2009. A bit more eco-friendly.

The mandatory CITCON Paris 2009 T-Shirt. I Think I probably have more T-Shirts than anything else in my closet, enough to wear a different one for each casual Friday of the year ;-) .

A JetBrains TeamCity T-Shirt. This one is kind of “home made”, don’t put it in your washing machine. JetBrains people are very good at making incredible softwares like IntelliJ IDEA and they definitely should stay in the software business, not in the T-Shirt business ;-) .

A note pad, useful for taking notes during sessions. Sponsored by Zutubi for their Pulse continuous integration system.

A ball!!! From Valtech. I don’t know why, but in every XP teams I’ve worked in, we always had some balls. I’m not sure that “useful” is an appropriate term for this device, but clearly a “must have” in a working team ;-) .

A 2GB USB flash drive from MSDN. As I already told before, 2GB is the minimum size we can get nowadays and since I don’t have bought any USB flash drive for years, this one is now the biggest one I have. I like the credit card format, it fit very well in a wallet but I don’t really like the way it’s done, it’s not really easy to use. I think the fictional one created by Marshall in Alias is more user friendly:

An evaluation CD from ThoughtWorks.

Some stuff to read, I trashed it all after fast-reading it (except the list of CITCON volunteers).

And of course, pens. A black one from Microsoft Hardware and a blue one from Open Information Foundation.

Conclusion
  • The bag: since there are goodies, we need something to hold them, but it should be done with more eco-friendly materials.
  • T-shirts: no really useful since we all already have tons of them and I think most of us can’t wear them very often.
  • Note pad and pens: Useful to take notes during sessions, but I think most of us bring our own ones.
  • Ball: that’s just for fun, actually it’s the goodies I use the most.
  • USB flash drive: the same bigger and more user friendly should be great (for me).
  • CD: come on, we can download your softwares from your website, we don’t need to have soon-outdated versions on a physical media.
  • Stuff to read: most of it was quite useless, at least it can be recycled.

It was not so bad this time, compared to some conferences where everything had to go to the trash directly.