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
  • 相关阅读:
    输入和输出插头
    MCB2300的CTM1050(CAN)
    POJ读书笔记2.1 —— 鸡兔笼带
    Java程序猿的书面采访String3
    JavaScript:undefined And null差异
    设计模式——结构模型
    Qt移动应用开发(六):QML与C++互动
    让我们来谈谈合并排序算法
    安装Eclipse完PyDev插件中没有出现
    汉顺平html5课程分享:6小时制作经典的坦克大战!
  • 原文地址:https://www.cnblogs.com/yfzhou528/p/14019478.html
Copyright © 2011-2022 走看看