Hi all,


I am currently writing a test case using Spring. Basically I have a method that should execute
under transaction (saveOrder() below). In this method, I delete any existing order, and then
check if the new order has any item (I know it should be the other way around, but for the
sake of my question.. lets do it this way).


Code:
public class ShoppingCartServicesImpl implements ShoppingCartServices{

	// this is a class that extends HibernateDaoSupport
	private CustomerOrderEntryDAO orderDAO;
	
	@Transactional(readOnly=false, rollbackFor = EmptyCustomerOrderEntryException.class)
    public void saveOrder(final CustomerOrderEntry order)
            throws EmptyCustomerOrderEntryException{
			
		
		orderDAO.deleteInProgressOrder(order.getUserId());
        
		
		if (order.getItems().size() < 1) {
            throw new EmptyCustomerOrderEntryException();
        }
		...
		..
			
	}
	
	@Transactional(readOnly=true)
	public CustomerOrderEntry loadInProgressOrder(final String customerId){
		...
	}
}
And here is my test case.

Code:
public class MyTest extends AbstractTransactionalDataSourceSpringContextTests{
  private ShoppingCartServices orderServices;

  public void testSaveInvalidOrderWithExistingInProgress() {
	..
	..
	CustomerOrderEntry currentInProgressOrder = orderServices.loadInProgressOrder("90238423");
	assertNotNull(currentInProgressOrder); // Works OK
	
	// Create a new empty order.. called "newOrder".
	CustomerOrderEntry newOrder = new CustomerOrderEntry();
	...
	..
	
	
	try {
            orderServices.saveOrder(newOrder);
            fail("Should not allow saving empty order"); // Works OK.. i.e. exception was thrown
	} catch (EmptyCustomerOrderEntryException e) {
        
	}
	...
	..
	CustomerOrderEntry currentInProgressOrder = orderServices.loadInProgressOrder("90238423");
	assertNotNull(currentInProgressOrder); // Assertion FAILED. Order was deleted!
  
  }
}

I cant understand why the 2nd assertion above failed. I have looked at the spring log, and clearly it stated
that the transaction rolled back ( due to EmptyCustomerOrderEntryException being thrown ).
Also, this method works fine when I ran it on the webapp (i.e. not as a test case).
My guess is: I should not use the following line:

Code:
CustomerOrderEntry currentInProgressOrder = orderServices.loadInProgressOrder("90238423");
to get the order. Since it will still execute under the same session where the order has been deleted, and this
session has not been rolled back (participating the transaction created by AbstractTransactionalDataSourceSpringContextTests) .
Am I right to say this? If so, could anyone please suggest a solution?


Thanks!