Junit 实例精讲基础教程(二) 使用@Test+expected、@Rule+ExpectedException模拟方法异常执行的方式

在实际业务中,进行单元测试时,我们除了需要测试正常流程和正确的程序功能之外,可能还需要测试异常场景,在Junit中该如何模拟异常场景呢?

更多精彩请阅读 东陆之滇的csdn博客:http://blog.csdn.net/zixiao217

在Junit中,通常有3种方式去模拟生产中的异常场景。

  • 使用@Test结合它的属性expected
  • 使用try-catch并且最后使用fail()
  • 使用@RuleExpectedException

1. 使用@Test和其属性expected

Exception1Test.java:

package org.byron4j.spring_mvc_log4j.mock;

import java.util.ArrayList;

import org.junit.Test;

public class Exception1Test {

    /**
     * 如果测试该方法时产生一个ArithmeticException的异常,则表示测试通过
     * 你可以改成int i = 1 / 1;运行时则会测试不通过-因为与你的期望的不符
     */
    @Test(expected = ArithmeticException.class)
    public void testDivisionWithException() {
        int i = 1 / 0;
    }

    /**
     * 运行时抛出一个IndexOutOfBoundsException异常才会测试通过
     */
    @Test(expected = IndexOutOfBoundsException.class)
    public void testEmptyList() {
        new ArrayList<>().get(0);
    }
}

2. try-catch并且总是在块最后加上fail()

Exception2Test.java:

package org.byron4j.spring_mvc_log4j.mock;

import java.util.ArrayList;

import org.junit.Test;

//注意:这是java中的静态引入
import static junit.framework.TestCase.fail;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class Exception2Test {
    @Test
    public void testDivisionWithException() {
        try {
            int i = 1 / 0;

           fail(); 
        } catch (ArithmeticException e) {
            assertThat(e.getMessage(), is("/ by zero"));

        }
    }



    @Test
    public void testEmptyList() {
        try {
            new ArrayList<>().get(0);
            fail();
        } catch (IndexOutOfBoundsException e) {
            assertThat(e.getMessage(), is("Index: 0, Size: 0"));
        }
    }
}

3. 使用@Rule和ExpectedException

ExpectedException从4.7之后才有的,可以让你测试到异常类型和异常信息。可以认为和try-catch+fail(),但是更优雅些。

Exception3Test.java:

package org.byron4j.spring_mvc_log4j.mock;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasProperty;



import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;


public class Exception3Test {
    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void testDivisionWithException() {

        thrown.expect(ArithmeticException.class);
        thrown.expectMessage(containsString("/ by zero"));

        int i = 1 / 0;

    }

    @Test
    public void testNameNotFoundException() throws NameNotFoundException {

        //test type
        thrown.expect(NameNotFoundException.class);

        //test message
        thrown.expectMessage(is("Name is empty!"));

        //test detail
        thrown.expect(hasProperty("errCode"));  //make sure getters n setters are defined.
        thrown.expect(hasProperty("errCode", is(666)));

        CustomerService cust = new CustomerService();
        cust.findByName("");

    }

}

自定义的异常类NameNotFoundException.java:

package org.byron4j.spring_mvc_log4j.mock;

public class NameNotFoundException extends Exception{
    private int errCode;

    public NameNotFoundException(int errCode, String message) {
        super(message);
        this.errCode = errCode;
    }

    public int getErrCode() {
        return errCode;
    }

    public void setErrCode(int errCode) {
        this.errCode = errCode;
    }
}

CustomerService.java:

package org.byron4j.spring_mvc_log4j.mock;



public class CustomerService {
    public Customer findByName(String name) throws NameNotFoundException {

        if ("".equals(name)) {
            throw new NameNotFoundException(666, "Name is empty!");
        }

        return new Customer(name);

    }

    static class Customer{
        private String name;

        public Customer(String name) {
            super();
            this.name = name;
        }


    }
}
  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值