Hi,
I'm a java programmer who is now using TDD and junit. In my current project, we are using webwork/spring/hibernate trioes.And in architecture
view, there is a web controller/ service / dao three layer logic separation. And there is another important layer called domain model. Sevice layer
usually delegate to domain model to complete it's service and it should be a thin layer. My question is how to test the web && service && domain model.After i have tested a particular business logic in domain logic, do i need to test corresponding conditions in service layer and web
layer.For example, in traditional auction application(citing caveatemptor application in book "Hibernate In Action"):
In this application, i will write four tests for placing bid in domain model layer.Code:public class Item implements Serializable { public Bid placeBid(User bidder, MonetaryAmount bidAmount, Bid currentMaxBid, Bid currentMinBid) throws BusinessException { // Check highest bid (can also be a different Strategy (pattern)) if (currentMaxBid != null && currentMaxBid.getAmount().compareTo(bidAmount) > 0) { throw new BusinessException("Bid too low."); } // Auction is active if ( !state.equals(ItemState.ACTIVE) ) throw new BusinessException("Auction is not active yet."); // Auction still valid if ( this.getEndDate().before( new Date() ) ) throw new BusinessException("Can't place new bid, auction already ended."); // Create new Bid Bid newBid = new Bid(bidAmount, this, bidder); // Place bid for this Item this.getBids.add(newBid); return newBid; } } public class ItemManager { private ItemDao itemDao; public void setItemDao(ItemDao itemDao) { this.itemDao = itemDao;} public Bid loadItemById(Long id) { itemDao.loadItemById(id); } public Collection listAllItems() { return itemDao.findAll(); } public Bid placeBid(Item item, User bidder, MonetaryAmount bidAmount, Bid currentMaxBid, Bid currentMinBid) throws BusinessException { item.placeBid(bidder, bidAmount, currentMaxBid, currentMinBid); itemDao.update(item); } }
After i test domain model , do i need to test all the corresponding conditions in service, like this?Code:public class ItemTest extends TestCase { public void testPlaceBid() { // all is well // no exception will be thrown } public void testPlaceBidTooLow() { // test placing bid when bid is too low, and there will a exception thrown. } public void testPlaceBidInactive() { // test placing bid when auction is not active yet, and there will a exception thrown. } public void testPlaceBidInvalidDate() { // test placing bid when auction already ended, and there will a exception thrown. } }
I think in both test cases, the tests is nearly same. And if do so, there will be many tests that look very same spreaded from domain model to service layer to web layer.But if i don't do that, it seems that there isn't sufficient tests for testing service api.Code:public class ItemManagerTest extends TestCase { public void testPlaceBid() { // normal // no exception will be thrown } public void testPlaceBidTooLow() { // test placing bid when bid is too low, and there will a exception thrown. } public void testPlaceBidInactive() { // test placing bid when auction is not active yet, and there will a exception thrown. } public void testPlaceBidInvalidDate() { // test placing bid when auction already ended, and there will a exception thrown. } }
I'm a newbie to tdd and junit so maybe this is a silly question for you.But anyway i need your help to clear my eyes.
Thanks a lot!


Reply With Quote