Category: Allerlei

Testing Enumerations in Java

Posted by – September 10, 2009

Since testing is so much fun, I decided to unit-test my enums too. Just in case someone (me) changes those values without telling anybody or whatever reason for testing you got. Suppose you have an enum like follows:

public enum Month {
    January,
    February,
    ...
    December;
} 

To test that you could just use the valueOf(String) method of any enumeration and see if the resulting object is null or not. So you would call

assertThat(Month.valueOf("January"), is(notNullValue()));

for every item in the enumeration and probably create different test methods for each of those calls. Instead of that you could also use parameterized tests (with @RunWith(Parameterized.class) annotation) but I don’t like constructors in test classes. So with the release of JUnit 4.5 we now got Theories which are similar but not the same as parameterized tests. Instead of parameters and constructors with private fields you use data points and that’s it basically. With that we can replace all those more or less similar test cases for every enumeration item with a single:

@Theory
public final void monthIsNotNull(final String month) {
    assertThat(Month.valueOf(month), is(notNullValue()));
}

What’s missing here is the already mentioned DataPoint. You declare one with the @DataPoint annotation or multiple data points with the @DataPoints annotation like follows:

@DataPoint
public static final String JANUARY = "January"

After that you have to annotate your test class with the @RunWith(Theories.class) and JUnit will automatically run the theory on all available data points. You can take a look here to see how I applied that to unit-testing inside the Denove-core package.

Tomboy

Posted by – February 5, 2009

Tomboy nutze ich schon seit einiger Zeit und hatte weder technische noch funktionale Probleme damit. Allerdings hatte ich seit dem Update auf Ubuntu 8.10 immer das Problem, dass ich das Suchfenster von Tomboy angezeigt bekommen habe, nachdem ich mich als Benutzer am System angemeldet habe. In der Regel keine große Sache, da man das Fenster ja einfach schließen kann. Nur blöd, dass dabei dann immer wieder mal (es war nie wirklich reproduzierbar) damit auch gleich Tomboy geschlossen wurde. Ein erneutest Starten von Tomboy brachte wieder das Suchfenster und das Schließen des Fenster schloss auch wieder Tomboy selbst usw.

Wie kommt man da raus ohne auf Tomboy zu verzichten? Ganz einfach die “–search” Option aus dem Starter Menü entfernen :-) Kleine Sache, große Wirkung.

Go-Ahead in Münster

Posted by – November 13, 2008

Nächsten Montag (17.11.2008) ist es endlich soweit und wir veranstalten unser erstes offizieles Go-Ahead! Münster Treffen. Getroffen wird sich in der Frauenstraße 24 ab halb 7 (18:30). Auf dem Tagunsplan steht wohl erstmal die Errichtung einer Münsteraner Splitergruppe der Go-Ahead! Organisation und dann anschließend 3-17 Biere. Freuen uns – wie immer – über unzählige Besucher…