Archive for the ‘Examples’ Category

EasyMock Capture Example

Thursday, September 2nd, 2010

Background

EasyMock version 2.4 introduced some new functionality – the ability to capture the arguments passed to mock objects. As ever a few lines of code speaks volumes.

Capture Example

Test Class

package foo.bar;

import static org.junit.Assert.assertEquals;

import org.easymock.Capture;
import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.junit.Before;
import org.junit.Test;

public class TestServiceImpl {
    private Service service;
    private Dao dao;
    private IMocksControl controller;
    private final String description = "description";

    @Before
    public void setUp() throws Exception {
        controller = EasyMock.createStrictControl();
        dao = controller.createMock(Dao.class);
        service = new ServiceImpl();
        service.setDao(dao);
    }
    @Test
    public void testSave() {
        Capture <DomainObject> capturedArgument = new Capture <DomainObject>();
        dao.save(EasyMock.and(EasyMock.capture(capturedArgument), EasyMock.isA(DomainObject.class)));
        controller.replay();
        service.save(description);
        controller.verify();
        DomainObject record = capturedArgument.getValue();
        assertEquals(description, record.getDescription());
        assertEquals("getNextId", record.getId());
    }
}

Source code for classes under test after the break…….
(more...)

Tags: , ,
Posted in Development, Examples | No Comments »

Using Ibatis Typehandlers to fix Oracle Date “missing time”

Friday, June 25th, 2010

The Problem

When using Ibatis to retrieve Date type values, on Oracle 10g (some driver versions), the time portion of the value returned from the database is not mapped onto the Java object.

i.e.

In DB: 02-JAN-10 13:30:00
In Java Object after mapping: 02-JAN-10 00:00:00

The Ibatis ResultMapper, snippet, looks like this:

....
<result property="savedDate" column="SAVED_DATE" javaType="java.util.Date" jdbcType="DATE"/>
....

(more...)

Tags: , ,
Posted in Examples | No Comments »

Unit Testing Spring apps with @RunWith(SpringJUnit4ClassRunner.class)

Thursday, June 24th, 2010

The Problem

I love Spring, who doesn’t?

One thing however that I found, until recently, a bit awkward was Unit Testing objects which were constructed and initiated via the Spring context and injected into other objects that consumed them. I have seen and used many and varied “bespoke” ways to do this, none of which I found satisfying.

This was until a collegue introduced me to the wonder that is SpringJUnit4ClassRunner. I know, I know, I should have been aware of this ages ago but as they say on millionaire “it’s easy if you know the answer”!
(more...)

Tags: , ,
Posted in Examples | No Comments »

Ibatis TypeHandlerCallback Enum Example

Friday, June 11th, 2010

Problem

You need Ibatis to perform custom processing before parameters are set on a Prepared Statement or after the results are retrieved from the result set. Maybe to convert Y/N to boolean, map results to custom objects etc

Solution

You probably want to look at implementing Ibatis’ TypeHandlerCallBack, the API docs for which are here
(more...)

Tags: ,
Posted in Examples | No Comments »

Java 5 Enum Example

Thursday, June 10th, 2010

Description

This simple example of a Java 5 Enum models a PREV/NEXT you might commonly see in a web app.

package foo.bar;

public enum PagingCriteriaDirection {
    NEXT("Next"), PREV("Previous");

    private String description;

    private PagingCriteriaDirection(String desc){
        description = desc;
    }

    public String getDescription() {
    	return description;
    }

    @Override
    public String toString() {
    	return description;
    }
}

Tags:
Posted in Development, Examples | 1 Comment »