Junit 实例精讲基础教程(二) 使用@Test+expected、@Rule+ExpectedException模拟方法异常执行的方式
2016-10-28 01:13
393 查看
在实际业务中,进行单元测试时,我们除了需要测试正常流程和正确的程序功能之外,可能还需要测试异常场景,在Junit中该如何模拟异常场景呢?
更多精彩请阅读 东陆之滇的csdn博客:http://blog.csdn.net/zixiao217
在Junit中,通常有3种方式去模拟生产中的异常场景。
使用@Test结合它的属性expected
使用try-catch并且最后使用fail()
使用@Rule和 ExpectedException
Exception3Test.java:
自定义的异常类NameNotFoundException.java:
CustomerService.java:
更多精彩请阅读 东陆之滇的csdn博客:http://blog.csdn.net/zixiao217
在Junit中,通常有3种方式去模拟生产中的异常场景。
使用@Test结合它的属性expected
使用try-catch并且最后使用fail()
使用@Rule和 ExpectedException
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; } } }
相关文章推荐
- Junit 实例精讲基础教程(一) 使用@Ignore注解跳过单元测试方法的执行
- Junit 实例精讲基础教程(三) 使用@Test(timeout=5000)模拟超时进行单元测试
- Junit 实例精讲基础教程(一) 认识Junit基本注解@Before、@After、@Test、@BeforeClass、@AfterClass
- Junit 实例精讲基础教程(一) 认识Junit基本注解@Before、@After、@Test、@BeforeClass、@AfterClass
- parasoft Jtest 使用教程:生成、执行和扩展Junit测试用例
- parasoft Jtest 使用教程:生成、执行和扩展Junit测试用例
- video4linux(v4l)使用摄像头的实例基础教程与体会(推荐)
- video4linux(v4l)使用摄像头的实例基础教程
- video4linux(v4l)使用摄像头的实例基础教程与体会
- video4linux(v4l)使用摄像头的实例基础教程与体会
- video4linux(v4l)使用摄像头的实例基础教程与体会
- video4linux(v4l)使用摄像头的实例基础教程与体会(三)
- video4linux(v4l)使用摄像头的实例基础教程与体会(四)
- 利用resteasy框架构建rest webservice----第二波:使用不同的方式让resteasy发布我们的restful webservice 服务(实例、教程)
- video4linux(v4l)使用摄像头的实例基础教程与体会
- video4linux(v4l)使用摄像头的实例基础教程与体会
- video4linux(v4l)使用摄像头的实例基础教程与体会(顶)
- video4linux(v4l)使用摄像头的实例基础教程与体会
- video4linux(v4l)使用摄像头的实例基础教程与体会
- video4linux(v4l)使用摄像头的实例基础教程与体会