PDA

View Full Version : Testing Persistence


Cameron Zemek
07-15-2003, 07:29 AM
How should you test the following (using DataAccessCommand pattern - Command
pattern for persistence):

public class ViewCart{
private CachedRowSet rowSet = null;
private int cartId;

public void setCartId(int cartId){
this.cartId = cartId;
}

public void execute() throws DataAccessException, LostConnectionException{
Connection conn = null;
PreparedStatement stat = null;
try{
Connection conn = ConnectionPool.getConnection();
stat = conn.prepareStatement(VIEW_CART_QUERY);
rowSet.populate(stat.executeQuery());
}catch(SQLException e){
throw new DataAccessException(e);
}finally{
try{ stat.close(); }catch(SQLException e){ }
try{ stat.close(); }catch(SQLException e){ throw new
LostConnectionException(e); }
}
}

public String getProductName(){
rowSet.getString("product_name");
}
// etc
}

Uncle Bob (Robert C. Martin)
07-17-2003, 11:29 AM
"Cameron Zemek" <grom_3@optusnet.com.au> might (or might not) have
written this on (or about) Wed, 16 Jul 2003 01:29:42 +1000, :
How should you test the following (using DataAccessCommand pattern - Commandpattern for persistence):public class ViewCart{ private CachedRowSet rowSet = null; private int cartId; public void setCartId(int cartId){ this.cartId = cartId; } public void execute() throws DataAccessException, LostConnectionException{ Connection conn = null; PreparedStatement stat = null; try{ Connection conn = ConnectionPool.getConnection(); stat = conn.prepareStatement(VIEW_CART_QUERY); rowSet.populate(stat.executeQuery()); }catch(SQLException e){ throw new DataAccessException(e); }finally{ try{ stat.close(); }catch(SQLException e){ } try{ stat.close(); }catch(SQLException e){ throw newLostConnectionException(e); } } } public String getProductName(){ rowSet.getString("product_name"); } // etc}


The test should load some dummy rows and then invoke the ViewCard and
see if getProductName returns the correct data. Then the test should
delete the dummy rows.

Why do you repeat the stat.close() statement?



Robert C. Martin | "Uncle Bob"
Object Mentor Inc.| unclebob @ objectmentor . com
PO Box 5757 | Tel: (800) 338-6716
565 Lakeview Pkwy | Fax: (847) 573-1658 | www.objectmentor.com
Suite 135 | | www.XProgramming.com
Vernon Hills, IL, | Training and Mentoring | www.junit.org
60061 | OO, XP, Java, C++, Python |

Malte Finsterwalder
07-20-2003, 11:18 AM
On Fri, 18 Jul 2003 16:16:41 +1200, Shane Mingins wrote:
I was just refactoring some of my persistence tests today ... I am using OJB... and so to test that my Persistence.store(object) method was workingcorrectly, I was storing the object, then clearing the cache that OJB usesto force a database read, and then using my Persistence.get(objectName)method to read back the object and check that all parts were there thatshould be.And as I was doing this I thought perhaps I should use some plain SQL andJDBC to check that the object persisted to the database in the correctmanner?What would you recommend?

What do you want to test?
Do you want to test that you can create an object and when you read it
later get the object back poperly? Then using create and read from the
object should do. If you want to make sure the database has a certain
format and has certain values, then you should user JDBC to test this.

So I would use the create, store and read methods from the object to
test that I get the object back properly. I don't care how this is
stored in the database, at least not in this test.

Greetings,
Malte


MyLounge.com Site Map
Forum: Cars, Cell Phone, Database, Games, Home Improvement, IT, Music, School, Sports, Web Design, Web Server, Weight Loss

The MyLounge.com forum is intended for informational use only and should not be relied upon and is not a substitute for any advice. The information contained on MyLounge.com are opinions and suggestions of members and is not a representation of the opinions of MyLounge.com. MyLounge.com does not warrant or vouch for the accuracy, completeness or usefulness of any postings or the qualifications of any person responding. Please consult a expert or seek the services of an attorney in your area for more accuracy on your specific situation. Please note that our forums also serve as mirrors to Usenet newsgroups. Many posts you see on our forums are made by newsgroup users who may not be members of MyLounge.com Term of Service