usermanagertest.java

来自「springlive ch7」· Java 代码 · 共 72 行

JAVA
72
字号
package org.appfuse.service;import junit.framework.TestCase;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.appfuse.dao.UserDAOTest;import org.appfuse.model.User;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.dao.DataAccessException;public class UserManagerTest extends TestCase {    private static Log log = LogFactory.getLog(UserManagerTest.class);    private ApplicationContext ctx;    private User user;    private UserManager mgr;    protected void setUp() throws Exception {        String[] paths = {"/WEB-INF/applicationContext*.xml"};        ctx = new ClassPathXmlApplicationContext(paths);        mgr = (UserManager) ctx.getBean("userManager");    }    protected void tearDown() throws Exception {        user = null;        mgr = null;    }    public void testAddAndRemoveUser() throws Exception {        user = new User();        user.setFirstName("Easter");        user.setLastName("Bunny");        user = mgr.saveUser(user);        assertTrue(user.getId() != null);        if (log.isDebugEnabled()) {            log.debug("removing user...");        }        String userId = user.getId().toString();        mgr.removeUser(userId);        try {            user = mgr.getUser(userId);            fail("User '" + userId + "' found in database");        } catch (DataAccessException dae) {            log.debug("Expected exception: " + dae.getMessage());            assertTrue(dae != null);        }    }    public void testWithValidationErrors() {        user = new User();        user.setFirstName("Bill");                try {            user = mgr.saveUser(user);            fail("Validation exception not thrown!");        } catch (Exception e) {            log.debug(e.getCause().getMessage());            assertNotNull(e.getCause());        }    }        public static void main(String[] args) {        junit.textui.TestRunner.run(UserDAOTest.class);    }}

⌨️ 快捷键说明

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