Blog Archives

Eclipse Tips: Extract Local Variable

When I teach my little Java TDD course, I quite often do live coding demos …especially if we’re running behind schedule and I want to catch up; I’ll skip an exercise and work through the solution. And one of the remarks I often get at the end is “thanks for the course, but what I also found useful was learning about how to use Eclipse effectively”.

Well, I guess I have used Eclipse for over 10 years now, so I suppose I am reasonably familiar with it. So I thought I’d show some of these tips by way of a series of screencasts.

The first one in the series is nice and simple: the “Extract Local Variable” refactoring, and its corollary, “Inline Variable”; something I use an awful lot when working on Apache Isis codebase. You’ll find it just below…

Eclipse Juno JEE Setup

My installation of Eclipse Juno seemed to have got its knickers in rather a twist, so just spent a “happy” hour reinstalling the damn thing.

Anyway, here’s what my setup currently consists of:

Read the rest of this entry

Mockito-like automocking and optional autowiring in JMock

I’m running my little TDD course again, and (as usual) it’s given rise to another small idea to make unit testing easier: provide Mockito-like automocking (using a @Mock annotation), and in addition perform autowiring of all mocks into the class under test. Read the rest of this entry

DB unit testing with dbUnit, JSON, HSQLDB and JUnit Rules

In this week’s run of my TDD course, I thought it would be interesting to write a little fixture to make it easier to use dbUnit.  My original thought was just to teach dbUnit about JSON, but it turns out that Lieven Doclo has done that already.  So I decided to go a step further and also combine dbUnit with JUnit Rules, and provide automatic bootstrapping of an HSQLDB in-memory object store.

Read the rest of this entry

JMock 2.6 – JUnit Rules and automocking using an @Mock annotation

I’ve been using Mockito a lot this year at work, but I still prefer JMock.  However, JMock could do with trimming some of its boilerplate, and I also thought it would be a good idea to use JUnit 4.7′s org.junit.rules.MethodRule.

I was about to go implement something to support Mockito’s @Mock annotation, but thought I’d browse the jmock.org website first.  It looks like Steve and Nat are ahead of me, and that JMock 2.6 will have the sort of support I need.

Read the rest of this entry

Contract test for value types

In my 2-day TDD course a couple of the things we go into are value types and of contract tests. It occurred to me recently (while writing a new value type within Apache Isis) that I really should have a contract test for any value type. That would allow me to easily check that the value type’s equals() method was an equivalence relation (ie symmetric, transitive, reflexive) and that its hashCode() is consistent with equals().

What I came up with is the ValueTypeContractTestAbstract<T> class. This is how you would use it to test that java.lang.String#equals() meets the equivalence relation:

Read the rest of this entry

Asserting on Object Graphs using Hamcrest and MVEL

I’ve been delivering my TDD course today, one topic of which is writing custom matchers in Hamcrest. One topic that came up was in asserting state within large object graphs, so it struck me that a nice general purpose matcher would be one that used an expression language to navigate the graph.

For example, given a customer that has an address, which has a city, which has a name, then it’d be nice to be able to say:

assertThat("London", navigableFrom(customer, "address.city.name"))

where navigableFrom() is the factory for the matcher.

So, without further ado, here’s a set of tests for such a matcher: Read the rest of this entry

EasyMock, JUnit @Rules and Hamcrest

Taking a break from my series on putting a custom UI on Naked Objects, in a week or two I’ll be running my TDD training course. The client this time wants EasyMock instead of JMock (which is what I normally teach), so I’ve spent an hour trying to figure out how to get the automatic verification (that JMock’s own JUnit runner gives) along with easily using Hamcrest matchers in expectations. I also wanted a way to easily switch between mocking concrete classes or just interfaces.

Anyway, here’s what I’ve cooked up. Given an object to test:

package com.mycompany;
import com.mycompany.TeaMaker.MilkAmount;
import com.mycompany.TeaMaker.SugarAmount;

public class TeaDrinker {
	private TeaMaker teaMaker;
	public void setTeaMaker(TeaMaker teaMaker) {
		this.teaMaker = teaMaker;
	}

	public void drinkTea() {
		teaMaker.makeTea(SugarAmount.ONE_SPOON, MilkAmount.JUST_A_DASH);
	}
}

and a collaborator (that we want to mock out):
Read the rest of this entry

JMock Actions API

I use JMock as my preferred mocking library. Most of the time the expectations I write are pretty simple, indicating that a mock should return this value, or should throw this exception. For example:

mockery.checking(new Expectations() {{
 allowing(mockConfiguration).getString(
     "nakedobjects.persistor.adapter-factory",
     PojoAdapterFactory.class.getName());
 will(returnValue(PojoAdapterFactory.class.getName()));
}});

However, I recently needed wrote a little API whereby Injectable components will inject themselves into any objects that declare itself to be aware of that component. For example, we have a SpecificationLoader:

Read the rest of this entry

Follow

Get every new post delivered to your Inbox.

Join 197 other followers