⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usermanagerjmtest.java

📁 springlive ch8
💻 JAVA
字号:
package org.appfuse.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.dao.UserDAO;
import org.appfuse.model.User;
import org.appfuse.service.impl.UserManagerImpl;
import org.appfuse.web.UserValidator;
import org.jmock.Mock;
import org.jmock.MockObjectTestCase;
import org.jmock.core.Stub;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ObjectRetrievalFailureException;

public class UserManagerJMTest extends MockObjectTestCase {
    private static Log log = LogFactory.getLog(UserManagerJMTest.class);
    private UserManager mgr = new UserManagerImpl();
    private Mock mockDAO = null;

    protected void setUp() throws Exception {
        mockDAO = new Mock(UserDAO.class);
        mgr.setValidator(new UserValidator());
        mgr.setUserDAO((UserDAO) mockDAO.proxy());
    }

    public void testAddAndRemoveUser() throws Exception {
        User user = new User();
        user.setFirstName("Easter");
        user.setLastName("Bunny");

        // set expected behavior on dao
        mockDAO.expects(once()).method("saveUser")
               .with( same(user) ).will(assignUserId());
        
        user = mgr.saveUser(user);
        
        // verify expectations
        mockDAO.verify();
        
        assertEquals(user.getFullName(), "Easter Bunny");

        assertNotNull(user.getId());

        if (log.isDebugEnabled()) {
            log.debug("removing user...");
        }

        String userId = user.getId().toString();
        
        mockDAO.expects(once()).method("removeUser")
               .with( eq(new Long(userId)) );
        
        mgr.removeUser(userId);

        // verify expectations
        mockDAO.verify();

        try {
            // set expectations
            Throwable ex = 
                new ObjectRetrievalFailureException(User.class, user.getId());
            mockDAO.expects(once()).method("getUser")
                   .with( eq(new Long(userId))).will(throwException(ex));
            
            user = mgr.getUser(userId);
            
            // verify expectations
            mockDAO.verify();
            fail("User '" + userId + "' found in database");
        } catch (DataAccessException dae) {
            log.debug("Expected exception: " + dae.getMessage());
            assertNotNull(dae);
        }
    }
    
    private Stub assignUserId() {
        return new AssignIdStub();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -