Maybe one of the best things of the MDA is that with a good template you can have all the test classes builded from just one click. Not to comment, the fact that the project will allways keep well documented with detailed uml :)
I used
topcased as modeling tool and
Acceleo as code generator.
It's very easy to extend the
JEE Hibernate/Struts module of Acceleo.
A
s a starting point, this module ships with a basic generation of junit-unit and html-unit components. But it's not hard to extend these components for generate more precise classes for testing. That will save us much of the tedious task of generating test classes
Here is a sample:
<%--
sso 03-2008
--%>
<%
metamodel http://www.eclipse.org/uml2/2.0.0/UML
import es.sso.uml21.common.common
import es.sso.uml21.common.specifics
import es.sso.uml21.common.dataTypes
import es.sso.uml21.common.structure
import es.sso.uml21.common.services.StringServices
import es.sso.uml21.common.services.ListServices
import es.sso.uml21.common.services.Uml2Services
%>
<%script type="Class" name="fullFilePath"%>
<%if (hasStereotype(getProperty("Entity"))){%>
/<%daoTestPackage.toPath()%>/<%name%>DaoTest.java
<%}%>
<%script type="Class" name="generate" file="<%fullFilePath%>"%>
package <%daoTestPackage%>;
// <%startUserCode%> for import
<%if (attribute.select("type.name == 'Date'")){%>
import java.util.Date;
<%}%>
import junit.framework.Assert;
import junit.framework.TestCase;
import java.sql.SQLException;
import org.apache.commons.beanutils.DynaBean;
import com.mockrunner.jdbc.StatementResultSetHandler;
import com.mockrunner.mock.jdbc.MockResultSet;
import es.lacaixa.intranet.commons.beanutils.LazyDynaBean;
import es.lacaixa.intranet.test.BasicDAOTest;
import <%daoPackage%>.<%name%>Dao;
// <%endUserCode%> for import
public class <%name%>DaoTest extends BasicDAOTest {
/**
* Test the insert of the entity <%name%>.<br/>
* <ul><li>Step 1 : Create an entity</li>
* <li>Step 2 : Verificar datos insert</li></ul>
*/
public void testInsert() {
<%name%>Dao <%name.toLowerCase()%>Dao= new <%name%>Dao();
DynaBean dynaBean = new LazyDynaBean();
// <%startUserCode%>
<%for (attribute){%>
<%if (type.name == "String"){%>
dynaBean.set("<%name%>","<%i()+1%>");
<%}else if(type.name == "Date"){%>
<%-- pValues.setDate(<%i()+1%>, new Date((String)dynaBean.get("<%name%>")); --%>
<%}else if(type.name == "Integer"){%>
dynaBean.set("<%name%>",new Integer(<%i()+1%>));
<%}%>
<%}%>
// <%endUserCode%>
<%name.toLowerCase()%>Dao.insert(dynaBean);
String sqlQuery = "INSERT INTO <%tablaBDD%>";
verifySQLStatementExecuted(sqlQuery+" (<%attribute.name.sep(", ")%>) VALUES (<%paramInserts.substring(0,paramInserts.length()-2)%>)");
verifySQLStatementNotExecuted("UPDATE INTO <%tablaBDD%>a");
int numDeParametr=1;
<%for (attribute){%>
<%if (type.name == "String"){%>
verifySQLStatementParameter(sqlQuery, 0, numDeParametr, "<%i()+1%>");
numDeParametr++;
<%}else if(type.name == "Date"){%>
<%-- pValues.setDate(<%i()+1%>, new Date((String)dynaBean.get("<%name%>")); --%>
<%-- numDeParametr++; --%>
<%}else if(type.name == "Integer"){%>
verifySQLStatementParameter(sqlQuery, 0, numDeParametr, new Integer(<%i()+1%>));
numDeParametr++;
<%}%>
<%}%>
verifyNotCommitted();
verifyAllResultSetsClosed();
verifyAllStatementsClosed();
//verifyConnectionClosed();
}
/**
* Test the update of the entity <%name%>.<br/>
* <ul><li>Step 1 : Create an entity</li>
* <li>Step 2 : Verificar datos update</li></ul>
*/
public final void testUpdate() {
<%name%>Dao <%name.toLowerCase()%>Dao= new <%name%>Dao();
DynaBean dynaBean = new LazyDynaBean();
// <%startUserCode%>
<%for (attribute){%>
<%if (type.name == "String"){%>
dynaBean.set("<%name%>","<%i()+1%>");
<%}else if(type.name == "Date"){%>
<%-- pValues.setDate(<%i()+1%>, new Date((String)dynaBean.get("<%name%>")); --%>
<%}else if(type.name == "Integer"){%>
dynaBean.set("<%name%>",new Integer(<%i()+1%>));
<%}%>
<%}%>
// <%endUserCode%>
<%name.toLowerCase()%>Dao.update(dynaBean);
String sqlQuery = "UPDATE <%tablaBDD%> SET <%attribute.genComparator.sep(", ")%>";
verifySQLStatementExecuted(sqlQuery);
verifySQLStatementNotExecuted("UPDATE INTO <%tablaBDD%>a");
int numDeParametr=1;
<%for (attribute){%>
<%if (isUnique){%>
<%if (type.name == "String"){%>
verifySQLStatementParameter(sqlQuery, 0, numDeParametr, String.valueOf(numDeParametr));
numDeParametr++;
<%}else if(type.name == "Date"){%>
<%-- pValues.setDate(<%i()+1%>, new Date((String)dynaBean.get("<%name%>")); --%>
<%-- numDeParametr++; --%>
<%}else if(type.name == "Integer"){%>
verifySQLStatementParameter(sqlQuery, 0, numDeParametr, new Integer(numDeParametr));
numDeParametr++;
<%}%>
<%}%>
<%}%>
verifyNotCommitted();
verifyAllResultSetsClosed();
verifyAllStatementsClosed();
//verifyConnectionClosed();
}
/**
* Test the update of the entity <%name%>.<br/>
* <ul><li>Step 1 : Create an entity</li>
* <li>Step 2 : Verificar datos update</li></ul>
*/
public final void testDelete() {
<%name%>Dao <%name.toLowerCase()%>Dao= new <%name%>Dao();
DynaBean dynaBean = new LazyDynaBean();
// <%startUserCode%>
<%for (attribute){%>
<%if (isUnique){%>
<%if (type.name == "String"){%>
dynaBean.set("<%name%>","<%i()+1%>");
<%}else if(type.name == "Date"){%>
<%-- pValues.setDate(<%i()+1%>, new Date((String)dynaBean.get("<%name%>")); --%>
<%}else if(type.name == "Integer"){%>
dynaBean.set("<%name%>",new Integer(<%i()+1%>));
<%}%>
<%}%>
<%}%>
// <%endUserCode%>
<%name.toLowerCase()%>Dao.delete(dynaBean);
String sqlQuery = "DELETE FROM <%tablaBDD%> WHERE ";
verifySQLStatementExecuted(sqlQuery);
verifySQLStatementNotExecuted("UPDATE INTO <%tablaBDD%>a");
// <%startUserCode%>
<%for (attribute){%>
<%if (isUnique){%>
<%if (type.name == "String"){%>
verifySQLStatementParameter(sqlQuery, 0,<%i()+1%>,"<%i()+1%>");
<%}else if(type.name == "Date"){%>
<%-- pValues.setDate(<%i()+1%>, new Date((String)dynaBean.get("<%name%>")); --%>
<%}else if(type.name == "Integer"){%>
verifySQLStatementParameter(sqlQuery, 0, <%i()+1%>,new Integer(<%i()+1%>));
<%}%>
<%}%>
<%}%>
// <%endUserCode%>
verifyNotCommitted();
verifyAllResultSetsClosed();
verifyAllStatementsClosed();
//verifyConnectionClosed();
}
/**
* Test the search by id para <%name%>.<br/>
*/
public final void testFindById() {
<%name%>Dao <%name.toLowerCase()%>Dao= new <%name%>Dao();
DynaBean dynaBean = new LazyDynaBean();
// <%startUserCode%>
<%for (attribute){%>
<%if (isUnique){%>
<%if (type.name == "String"){%>
dynaBean.set("<%name%>","<%i()+1%>");
<%}else if(type.name == "Date"){%>
<%-- pValues.setDate(<%i()+1%>, new Date((String)dynaBean.get("<%name%>")); --%>
<%}else if(type.name == "Integer"){%>
dynaBean.set("<%name%>",new Integer(<%i()+1%>));
<%}%>
<%}%>
<%}%>
// <%endUserCode%>
<%name.toLowerCase()%>Dao.findById(dynaBean);
String sqlQuery = "SELECT <%attribute.name.sep(", ")%> FROM <%tablaBDD%> WHERE ";
verifySQLStatementExecuted(sqlQuery);
verifySQLStatementNotExecuted("UPDATE INTO <%tablaBDD%>");
// <%startUserCode%>
<%for (attribute){%>
<%if (isUnique){%>
<%if (type.name == "String"){%>
verifySQLStatementParameter(sqlQuery, 0,<%i()+1%>,"<%i()+1%>");
<%}else if(type.name == "Date"){%>
<%-- pValues.setDate(<%i()+1%>, new Date((String)dynaBean.get("<%name%>")); --%>
<%}else if(type.name == "Integer"){%>
verifySQLStatementParameter(sqlQuery, 0, <%i()+1%>,new Integer(<%i()+1%>));
<%}%>
<%}%>
<%}%>
// <%endUserCode%>
verifyNotCommitted();
verifyAllResultSetsClosed();
verifyAllStatementsClosed();
//verifyConnectionClosed();
}
}
<%script type="Property" name="exempleValues"%>
<%if (type.name == "Integer"){%><%name.uniqueCode()%><%}else{%>
<%if (type.name == "String"){%>"my<%name.firstUpper()%>"<%}else{%>
<%if (type.name == "Boolean"){%>true<%}else{%>
<%if (type.name == "Date"){%>new Date(0)<%}else{%>
<%if (type.name == "Float"){%><%name.uniqueCode()%>.0<%}else{%>
new <%type.name%>()<%}%><%}%><%}%><%}%><%}%>
<%script type="Property" name="exempleValues2"%>
<%if (type.name == "Integer"){%><%name.uniqueCode()%><%}else{%>
<%if (type.name == "String"){%>"second<%name.firstUpper()%>"<%}else{%>
<%if (type.name == "Boolean"){%>false<%}else{%>
<%if (type.name == "Date"){%>new Date(1)<%}else{%>
<%if (type.name == "Float"){%><%name.uniqueCode()%>.0<%}else{%>
new <%type.name%>()<%}%><%}%><%}%><%}%><%}%>