1. quickstart

本文介绍了一个基于Mockito的单元测试案例,针对登录控制器进行成功登录、失败登录及异常情况的测试。通过模拟请求参数和数据库操作,验证控制器的行为是否符合预期。
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
### 回答1: Telerik QuickStart 是一种开发工具,用于加速开发者在构建应用程序时的过程。它是由Progress Software公司推出的一套软件开发工具包(SDK)。 Telerik QuickStart 的主要目标是为开发人员提供一个简单且高效的开发环境,以快速构建各种类型的应用程序,包括桌面应用程序、Web应用程序和移动应用程序。 Telerik QuickStart 提供了丰富的开发工具和组件,如UI控件、数据访问工具、报表生成工具等。这些工具可以帮助开发者轻松实现应用程序的各种功能和特性,并提高开发效率。同时,Telerik QuickStart 还支持多种编程语言,包括C#、VB.NET、JavaScript等。 通过使用 Telerik QuickStart,开发人员可以极大地简化开发过程,减少重复工作,并提高代码的质量和可维护性。它提供了丰富的示例代码和文档,帮助开发人员快速入门并掌握各种开发技巧和最佳实践。 此外,Telerik QuickStart 还提供了一套强大的调试和测试工具,以确保开发的应用程序能够稳定运行和满足用户需求。开发人员可以利用这些工具进行代码分析、性能优化和错误调试等。 总之,Telerik QuickStart 是一款功能强大且易于使用的开发工具,为开发人员提供了一套完整的解决方案,帮助他们快速构建高质量的应用程序。 ### 回答2: telerik.quickstart是一个针对.NET开发的快速启动工具。它是Telerik公司开发的一个用于快速创建和定制.NET应用程序的工具集合。telerik.quickstart提供了许多预定义的组件和模块,开发人员可以根据自己的需求选择和使用。 telerik.quickstart包含丰富的UI组件,如按钮、文本框、下拉列表和表格等,开发人员可以直接使用这些组件来构建用户界面。它还提供了一些常见的功能模块,如数据访问、授权认证和日志记录等,开发人员可以通过简单的配置和集成来实现这些功能。 除了基础组件和功能模块,telerik.quickstart还提供了许多扩展和定制选项。开发人员可以使用它们来优化应用程序性能、增加用户体验和提高代码质量。 使用telerik.quickstart开发.NET应用程序有许多好处。首先,它提供了丰富的组件和功能模块,可以大大加快开发速度。其次,它支持跨平台开发,可以在Windows、Mac和Linux等操作系统上工作。最后,telerik.quickstart提供了详细的文档和示例,开发人员可以通过学习和参考这些资源来更好地使用该工具。 总的来说,telerik.quickstart是一个功能强大、易于使用且高效的.NET开发工具,可以帮助开发人员快速创建和定制.NET应用程序,提高开发效率和应用程序质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值