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

📄 usermanageremtest.java

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

import junit.framework.TestCase;

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.easymock.MockControl;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ObjectRetrievalFailureException;

public class UserManagerEMTest extends TestCase {
    private static Log log = LogFactory.getLog(UserManagerEMTest.class);
    private UserManager mgr = new UserManagerImpl();
    private MockControl control;
    private UserDAO mockDAO;

    protected void setUp() throws Exception {
        control = MockControl.createControl(UserDAO.class); // 1
        mockDAO = (UserDAO) control.getMock(); // 2
        mgr.setValidator(new UserValidator());
        mgr.setUserDAO(mockDAO);
    }
    
    public void testAddAndRemoveUser() throws Exception {
        User user = new User();
        user.setFirstName("Easter");
        user.setLastName("Bunny");

        // set expected behavior on dao
        mockDAO.saveUser(user);
        control.setVoidCallable();
        
        // switch from record to playback
        control.replay();
        
        user = mgr.saveUser(user);
        assertEquals(user.getFullName(), "Easter Bunny");
        control.verify();

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

        // set userId since Hibernate doesn't do it
        String userId = "1";
        user.setId(new Long(userId));
        
        // reset to record state
        control.reset();
        mockDAO.removeUser(new Long(1));
        control.setVoidCallable();
        control.replay();
        
        mgr.removeUser(userId);
        
        control.verify();

        try {
            // reset to record state
            control.reset();
            control.expectAndThrow(mockDAO.getUser(user.getId()), 
                    new ObjectRetrievalFailureException(User.class, user.getId()));
            // switch to playback
            control.replay();
            
            user = mgr.getUser(userId);
            
            control.verify();
            fail("User '" + userId + "' found in database");
        } catch (DataAccessException dae) {
            log.debug("Expected exception: " + dae.getMessage());
            assertNotNull(dae);
        }
    }
}

⌨️ 快捷键说明

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