Hamcrest Collection Matching with Junit 4.8

Sometime back I wrote about some testing anti-patterns.  Recently I came back to the project and made some trivial changes.  I fixed a bug upgraded spring and junit and migrated the test assertions to hamcrest fluid style.  The full project is available on git.

I find hamcrest to be a much cleaner way of writing assertions.  This test demonstrates the use of its fluid interface with some simple collection assertions.  There are newer versions of Hamcrest and the collection package may have moved into core, but the version I use here (1.0) was available on maven.  Note the integration with Junit.  The hasItem and hasItems methods are sourced from Junit 4.8.1.

package org.testpatterns.hamcrestexamples;

import java.util.Arrays;
import org.junit.Test;
import static org.hamcrest.collection.IsArrayContaining.hasItemInArray;
import static org.junit.matchers.JUnitMatchers.*;
import static org.junit.Assert.assertThat;

public class CollectionExamples {

  //Hamcrest with Collections and Arrays:
  static final String[] array = { "A", "B", "C" };
  static final List<String> list = Arrays.asList(array);

  @Test
  public void oneThingInArray () {
    assertThat(array, hasItemInArray("A"));
  }

  @Test
  public void arrayOfItemsInList () {
  String[] expected = { "A", "B", "C" };
    assertThat(list, hasItems(expected));
  }

  @Test
  public void itemInAList () {
    assertThat(list, hasItem("A"));
  }

  @Test
  public void itemsInAList () {
    assertThat(list, hasItems("A", "C"));
  }

}
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Achieving Test Driven Nirvana

Car Cuba

Cuban Car: Martin Harris

Well perhaps not Nirvana then, but at least having a suitable level of test coverage.  ;-)

I wanted to write an article around the uptake of test driven development. Scrum and agile are hard to do well. If you break these down, you often find that the components are pretty challenging too. TDD is difficult but it has gained widespread recognition at the intellectual level.  On the ground though the practice can be patchy. Why?

Before writing this article I had a look around to see what else had been written.  This article on Geiger’s Counterpoint sums up my thoughts exactly.  Its such a good article that I hardly have anything left to say.  In fact neatly I can just provide some bullet points!

Continue reading

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

KeySet v EntrySet code tidy

Its astounding how often I find maps being used like this.

for(String name: names.keySet()) {
    mymap.put(mymap.get(name),name);
}

Its inefficient because you have to fetch the keys and perform a lookup in the map with the key.

Instead using the EntrySet you can get all the keys and values in one hit. This saves having to perform the map lookup.

for (Entry<String, String> entry : entrySet) {
    mymap.put(entry.getValue(),entry.getKey());
}
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Diary of a cloud backup – part 4 – Crashplan

Apologies for posting yet another backup page.  There are a few people interested in the outcome, although I suspect even they are bored!

After my terrible experience with memopal, I decided to look at crashplan.  Manung Han at Lab49 had a good experience.  Crashplan was the next best offer price when I evaluated the first time around.  This time I don’t need to go over the ADSL issues etc.  If your interested in what happened last time I attempted an offline backup read on.  I am going to keep this article short and to the point for fear of overloading syndicated websites like Lab49 with backup pages.  I will post updates to this final page as I go.

Strategy

After the last attempt I re-thought my strategy.  The new idea is to find a service that is free for the first month.  Then attempt backups with Linux, Mac and Windows.  These will not be full backups but big enough to test the system.

Continue reading

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)