3 June 2009

Practical Unit Testing

In the beginning of June I gave a presentation about practical unit testing with JUnit at the Java Student User Group in Vienna. JSUG is a small group of dedicated students that formed last year. The scope was practical as well as pragmatic - in fact just bits of information I considered useful for the daily development. It was good to see young developers that are eager to write tests. I hope when they are thrown into legacy code later in their career that their principles will not just crumble.

Download the Practical Unit Testing presentation slides.

Projector: Capitol Theatre in WestbankResources used in the slides:
This section contains links to topics I talked about. I give a list of all my sources. Hopefully Google does not punish me for creating this link-farm like page ;-)

JUnit basics used sources from Unit testing (Wikipedia), JUnit.org & JUnit, Early look at JUnit 4, Design to Unit Test, Test-driven design, Part 1 & Part 2; Checkstyle, FindBugs and PMD.

The mocking chapter used sources from jMock, EasyMock (Easier testing with EasyMock), Initializing bean using EasyMock, Mocking & Spring, Oh no, we're testing the Mock! and the Law Of Demeter.

Singletons are a pain for testing: Patterns I Hate, Singletons Are Evil and Why Singletons are Evil, Refactor singleton, Test flexibly with AspectJ and mock objects with AspectJ and the Google Singleton Detector.

There are some tools to help testing J2EE apps: HtmlUnit/HttpUnit, HttpClient, Testing Servlets and ServletUnit, Jetty, Cactus, Simple-JNDI, MockEJB and ActiveMQ.

Tuning the tests means often tuning the database: DbUnit, H2 and HSQLDB.

The Code Coverage chapter talks about EMMA (EclEmma), Cobertura, Agitar but Don't be fooled by the coverage report, Crap4j, Testability Explorer.

A new trend is testing with scripts: Unit test your Java code faster with Groovy or Using JRuby for Java testing, RSpec, JRuby, JtestR and ScalaCheck/Specs.

Finally some cool tools for testing and the build are JUnitPerf, SWTBot, XmlUnit, Distributed JUnit and GridGain.

29 May 2009

Public Relations

It seems that I am particularly unlucky in finding support for my interest in code quality. I can't help ranting so I will tell you this little story.

Some years ago, shortly after my "career" as code cop began, I wrote a series of articles about code quality and daily build techniques for a Java magazine (which I enjoy to do from time to time). My boss knew about it, but was not interested in it and I wrote the articles in my free time. (As you might have noticed I am not a gifted writer and filling all the gaps between facts and source code took the best part of most weekends during that year.) The first part was published soon after that.

Depressing DayAfter some time the second part of the series was published. This time the head of the IT department (the boss of my boss) learned about it and was impressed. He thought I had done it during work hours, or at least should have. I was happy and thankful for his appreciation and of course did not object to being paid for the time already spent. But my boss did not like the idea. He kept saying that we (the company I was with back then) were not an IT providing company and did not need to show any technical expertise or qualification to the outside world. However as he was forced to pay me for those hours, a rather difficult time began. In the end I regretted having shown the article to anyone.

In fact the whole story depressed me a lot and made me accept an offer from another company half a year later. There, during my interview I was promised support for writing technical articles. Later when a new article was due I wanted to call on that. It turned out that neither a budget nor a process was available for such activities so the support melted down to being allowed to write in my free time. I was pissed but in the end couldn't blame my boss. It was entirely my fault. Due to my lack of bargaining skills I hadn't pinned down the promised support to something concrete. (Technically my boss had not lied to me.) So from this time on I did neither ask nor expect any support. If they didn't want it, they should have said it straight at the first place.

But that was not the end...
Several months later I attended a department meeting on improving internal education, knowledge transfer and being more professional in general. It turned out that the head of the department (the boss of my boss) wanted more activities seen by the general public and that they had problems fulfilling the request of upper management to place an article in internal journals from time to time. Imagine my confusion when I heard that.

Yesterday a colleague brought a small, local conference on software quality to my attention (shame on me I hadn't known it before) and proposed that I should try to submit a talk. Again I tried to get some support for it. I am able to prepare stuff at home (at the weekends) but I can't afford taking days off for giving a talk. This time I contacted the boss of my boss directly because I remembered his (feigned?) interest in public relations. Unfortunately he delegated the decision back to my boss. As we already know, she does not deem public actions necessary. :-( My colleague was surprised, too. We remembered the things being discussed during the departmental meeting and both had thought that management wanted more public relations. (Otherwise I would have started questioning my senses.)

Depressed Wander
What is going on here? Are they all dreamers, talking about things they would like to have knowing they can never afford them? Or is it just another form of the suppressing code quality because we don't have any-syndrom? (In full name I call this the suppressing code quality topics because we know that we do not have any quality and I will write about it later.) I don't know. But I do know that it was the last time they tricked me. (They've already tricked me twice, shame on me.)

2 May 2009

Fragments of cool code

From time to time I stumble over a piece of code, that just looks "cool". For example date literals in Ruby:
class Fixnum
def /(part)
[self, part]
end
end

class Array
def /(final_part)
Time.mktime(final_part, self[1], self[0])
end
end

13/11/2008 # => Thu Nov 13 00:00:00 +0100 2008
or a proper name for a throw:
catch (Exception up) {
// log the error
throw up;
}
The best name for a JUnit 4 test fixture (free to choose) I ever saw was in a presentation given by Peter Petrov:
@Before
public void doBeforeEachTest() {
A good name for a Ruby binding was
module Regenerated
Inside = binding
end
eval(script, Regenerated::Inside) where the evaluation is done within the scope of another module Regenerated which acts as a namespace.