zoukankan      html  css  js  c++  java
  • 1. quickstart

    package lesson1_quickstart;
    
    import common.Account;
    import common.AccountDao;
    import common.AccountLoginController;
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.Mockito;
    import org.mockito.runners.MockitoJUnitRunner;
    import javax.servlet.http.HttpServletRequest;
    import static org.mockito.Matchers.anyString;
    import static org.mockito.Mockito.when;
    
    
    @RunWith(MockitoJUnitRunner.class)
    public class AccountLoginControllerTest {
    
        private AccountDao accountDao;
        private HttpServletRequest request;
        private AccountLoginController accountLoginController;
    
        @Before
        public void setUp() {
            accountDao = Mockito.mock(AccountDao.class);
            request = Mockito.mock(HttpServletRequest.class);
            accountLoginController = new AccountLoginController(accountDao);
        }
    
        @Test
        public void testLoginSuccess() {
            when(request.getParameter("username")).thenReturn("cain");
            when(request.getParameter("password")).thenReturn("123456");
    
            Account account = new Account();
            when(accountDao.getAccount(anyString(), anyString())).thenReturn(account);
    
            String login = accountLoginController.login(request);
            Assert.assertEquals("/index", login);
        }
    
        @Test
        public void testLoginFailure() {
            when(request.getParameter("username")).thenReturn("cain");
            when(request.getParameter("password")).thenReturn("123456");
    
            when(accountDao.getAccount(anyString(), anyString())).thenReturn(null);
    
            String login = accountLoginController.login(request);
            Assert.assertEquals("/login", login);
        }
    
        @Test
        public void test505() {
            when(request.getParameter("username")).thenReturn("cain");
            when(request.getParameter("password")).thenReturn("123456");
    
            when(accountDao.getAccount(anyString(), anyString())).thenThrow(UnsupportedOperationException.class);
    
            String login = accountLoginController.login(request);
            Assert.assertEquals("500", login);
        }
    }
    AccountLoginControllerTest
    package common;
    
    import javax.servlet.http.HttpServletRequest;
    
    public class AccountLoginController {
        private AccountDao accountDao;
    
        public AccountLoginController(AccountDao accountDao) {
            this.accountDao = accountDao;
        }
    
        public String login(HttpServletRequest request) {
            final String username = request.getParameter("username");
            final String password = request.getParameter("password");
            try {
                Account account = accountDao.getAccount(username, password);
                if (account == null) {
                    return "/login";
                } else {
                    return "/index";
                }
            } catch (Exception e) {
                return "500";
            }
    
        }
    }
    AccountLoginController
    package common;
    
    public class AccountDao {
        public Account getAccount(String username, String pwd) {
            throw new UnsupportedOperationException("DB is down");
        }
    }
    AccountDao
    package common;
    
    public class Account {
    
    }
    Account
  • 相关阅读:
    NYOJ 32(组合数)
    NYOJ 289(01背包)
    批量修改文件(图片)名称
    解决IIS7虚拟目录出现HTTP 错误 500.19(由于权限不足而无法读取配置文件)的问题
    MPP(最下正周期)
    wcf学习网站
    winform中用户输入查询与拼音首字母的结合,提高用户的操作体验 (转)
    通过SvcUtil.exe生成客户端代码和配置(转)
    WinMail 搭建邮件服务器。
    quick easy ftp server软件在机子上架设了个服务器
  • 原文地址:https://www.cnblogs.com/yfzhou528/p/14019478.html
Copyright © 2011-2022 走看看