Dear list,

I am using appfuse with Spring and Hibernate and i am having kind of the same 
problem as 
Paul 
http://www.nabble.com/ActionTest---Not-executing-sql-queries-on-Action.class-tp23680254s2369p23753651.html.

So i have the following class 

public class Dataset extends BaseObject {

    private Long id; 
    private User owner;
    
    public Dataset{
    }
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }
    @ManyToOne
    @JoinColumn(name = "owner_id", nullable = false)
    @org.hibernate.annotations.ForeignKey(name = "FK_DATASET_OWNER_ID")
    public User getOwner() {
        return owner;
    }

    //i have omitted the rest of the method and attributes for clarity

    public void setId(Long id) {
        this.id = id;
    }
    
    public void setOwner(User owner) {
        this.owner = owner;
    }
}


As you can see that i have a ForeignKey reference on User.
When i run tests at the DAO level where i try to remove a User that is 
referenced 
by a Dataset, as expected a ConstraintViolationException is thrown, which i can 
nicely handle in a catch block.
But when i run the same test at the Service level(the manager is only calling 
the DAO's remove method)
it fails because no exception is thrown.

If i set the defaultRollback to false, a ConstraintViolationException is thrown 
but outside my try-catch block.

So i was wondering if there is a way to do this kind of tests in the Manager, 
and what is the reason behind this behavior??

Here is my DAO test 

public class DatasetDaoTest extends BaseDaoTestCase {

    private DatasetDao dsDAO;
    private UserDao userDAo;
    
    public void setDatasetDao(DatasetDao datasetDao) {
        this.dsDAO = datasetDao;
    }
    
    public void setUserDao(UserDao userDao) {
        this.userDAo = userDao;
    }
    
    public void testFailToRemoveUser() {
        Dataset dataset = dsDAO.get(-1L);
        
        User owner = dataset.getOwner();
        
        assertNotNull(owner);
        
        try {
            userDAo.remove(owner.getId());
            flush();// of course not flushing the session will make the test to 
fail
            fail("ConstraintViolationException is not thrown ");
        } catch (Exception e) {
            log.debug(e.getMessage());
        }
    }
}


Here is the ManagerTest

public class DatasetManagerTest extends BaseDaoTestCase {

    private DatasetManager datasetManager;
    private UserManager userManager;

    public void setDatasetManager(DatasetManager datasetManager) {
        this.datasetManager = datasetManager;
    }
    
    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }    
    
    public void testFailToRemoveUser() {
        this.setDefaultRollback(false);
        Dataset dataset = datasetManager.get(-1L);
        
        User owner = dataset.getOwner();
        
        assertNotNull(owner);
        
        try {
            userManager.removeUser(owner.getId().toString());
            // commenting the following instruction will throw a 
ConstraintViolationException BUT OUT OF THIS try-catch block
            fail("ConstraintViolationException is not thrown ");
        } catch (Exception e) {
            log.debug(e.getMessage());
        }
    }
}

DEBUG - DatasetManagerTest.testFailToRemoveUser(589) | START FailToRemoveUser
DEBUG - UserManagerImpl.removeUser(117) | removing user: -1
DEBUG - DatasetManagerTest.testFailToRemoveUser(600) | User removed
DEBUG - DatasetManagerTest.testFailToRemoveUser(607) | STOP FailToRemoveUser
WARN - JDBCExceptionReporter.logExceptions(77) | SQL Error: 1451, SQLState: 
23000

thanks,

Youssef
_________________________________________________________________
Drag n’ drop—Get easy photo sharing with Windows Live™ Photos.

http://www.microsoft.com/windows/windowslive/products/photos.aspx

Reply via email to