• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

单元测试中JUnit中如何测试异常

发布: 2009-3-30 12:31 | 作者: 不详 | 来源: 测试时代采编 | 查看: 158次 | 进入软件测试论坛讨论

领测软件测试网

单元测试JUnit中如何测试异常?很多时候,我们要写一些单元测试来测试我们程序是否能正确触发异常。
  比如下面的例子中,我们就写了一个test case来测试一个Email验证类EmailAddrValidator,这个类有一个doValidate(email)方法可以验证email是否合法,如果不合法则会抛出ValidationException异常。因此我们写了两个方法来进行单元测试,前一个方法testDoValidate用来测试正常值,后一个方法testDoValidateException用来测试对错误的email格式是否能正确触发异常。
  这个例子的关键是方法testDoValidateException(String email) 。

import junit.framework.TestCase;

public class TestEmailAddrValidator extends TestCase {
    EmailAddrValidator validator = new EmailAddrValidator();

    public void testDoValidate() throws ValidationException {
        validator.doValidate("glchengang@163.com", null);
        validator.doValidate("glchen.gang@163.com", null);
        validator.doValidate("glchen_gang@163.com", null);
        validator.doValidate("glchen.gang@163_tom.com", null);
    }

    public void testDoValidateException() {
        testDoValidateException("@b.c");
        testDoValidateException("a@.c");
        testDoValidateException("a@b.");
        testDoValidateException("@.c");
        testDoValidateException("@...");
        testDoValidateException(" ");
        testDoValidateException(null);
    }

    private void testDoValidateException(String email) {
        try {
            validator.doValidate(email, null);
            fail("末抛出异常");
        } catch (ValidationException e) {
            assertTrue(true);
        }
    }
}



-----------------------------------------
注:在这里Locale 参数并没有用到。
import java.util.Locale;

import com.hygensoft.common.configure.ConfigureObject;

public class EmailAddrValidator{

    protected static final String ERROR_CODE_INVALID_EMAIL_ADDR = "INVALID_EMAIL_ADDR";
    protected static final String ERROR_CODE_INVALID_INPUT = "INVALID_INPUT_OBJECT";

    public Object doValidate(Object input, Locale locale) throws ValidationException {
        if (!(input instanceof String)) {
            throw new ValidationException(ERROR_CODE_INVALID_INPUT, input);
        }
        String inputStr = (String) input;
        int idx = inputStr.indexOf('@');
        if (idx == -1 || idx == 0) {
            throw new ValidationException(ERROR_CODE_INVALID_INPUT, input);
        }
        int idx2 = inputStr.indexOf('.', idx);
        if (idx2 == -1 || idx2 == idx + 1) {
            throw new ValidationException(ERROR_CODE_INVALID_INPUT, input);
        }
        if (inputStr.endsWith(".")) {
            throw new ValidationException(ERROR_CODE_INVALID_INPUT, input);
        }
        return input;
    }

    /* (non-Javadoc)
     * @see com.hygensoft.common.configure.Configurable#initialize(com.hygensoft.common.configure.ConfigureObject)
     */
    public void initialize(ConfigureObject conf) {}

}

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/

TAG: junit Junit JUnit JUNIT 单元


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网