Saturday, January 16, 2010

Test Scaffold

I must admit that when I use Sculptor I often skip completing the JUnit tests for the ordinary CRUD operations. To my defense I would say that they are trivial and completely generated, but anyway I think that is a bad habit that should be changed. Therefore I wrote a small Eclipse template for them. Maybe useful for you also:

public void testFindById() throws Exception {
${DomainObject:localVar(org.fornax.cartridges.sculptor.framework.domain.AbstractDomainObject)} obj = ${service:field}.findById(getServiceContext(), 1L);

public void testFindByIdWithNotFoundException() throws Exception {
${service}.findById(getServiceContext(), -1L);

public void testFindAll() throws Exception {
${listType:newType(java.util.List)}<${DomainObject}> found = ${service}.findAll(getServiceContext());
assertEquals(1, found.size());

public void testSave() throws Exception {
int countBefore = countRowsInTable(${DomainObject}.class);
${DomainObject} obj = new ${DomainObject}();
// TODO complete...
// obj.set${cursor}
${service}.save(getServiceContext(), obj);
assertEquals(countBefore + 1, countRowsInTable(${DomainObject}.class));

public void testDelete() throws Exception {
int countBefore = countRowsInTable(${DomainObject}.class);
${DomainObject} obj = ${service}.findById(getServiceContext(), 1L);
${service}.delete(getServiceContext(), obj);
assertEquals(countBefore - 1, countRowsInTable(${DomainObject}.class));

Add it in Eclipse > Preferences > Java > Editor > Templates
Name: testScaffold
Context: Java type members
Description: JUnit test methods for Sculptor CRUD operations

1 comment:

  1. Excellent read. I like your style...have a good one!/Nice blog! Keep it up!