java中junit处理异常,Junit中处理异常的另一种方式:catch-exception

在Junit中处理异常的方式有很多种,比如:

有人推荐我尝试一下catch-exception,在这篇文章中我将会向大家介绍。简而言之,catch-exception库可以仅在一行代码中捕获异常,以后再对其进行处理。

通过Maven安装

com.googlecode.catch-exception

catch-exception

1.2.0

test

依存关系树如下:

[INFO] --- maven-dependency-plugin:2.1:tree @ unit-testing-demo ---

[INFO] com.github.kolorobot:unit-testing-demo:jar:1.0.0-SNAPSHOT

[INFO] +- org.slf4j:slf4j-api:jar:1.5.10:compile

[INFO] +- org.slf4j:jcl-over-slf4j:jar:1.5.10:runtime

[INFO] +- org.slf4j:slf4j-log4j12:jar:1.5.10:runtime

[INFO] +- log4j:log4j:jar:1.2.15:runtime

[INFO] +- junit:junit:jar:4.11:test

[INFO] +- org.mockito:mockito-core:jar:1.9.5:test

[INFO] +- org.assertj:assertj-core:jar:1.5.0:test

[INFO] +- org.hamcrest:hamcrest-core:jar:1.3:test

[INFO] +- org.hamcrest:hamcrest-library:jar:1.3:test

[INFO] +- org.objenesis:objenesis:jar:1.3:test

[INFO] \- com.googlecode.catch-exception:catch-exception:jar:1.2.0:test

准备开始

被测系统(SUT):

class ExceptionThrower {

void someMethod() {

throw new RuntimeException("Runtime exception occurred");

}

void someOtherMethod() {

throw new RuntimeException("Runtime exception occurred",

new IllegalStateException("Illegal state"));

}

void yetAnotherMethod(int code) {

throw new CustomException(code);

}

}

使用AssertJ断言的BDD-style方法catch-exception示例:

import org.junit.Test;

import static com.googlecode.catchexception.CatchException.*;

import static com.googlecode.catchexception.apis.CatchExceptionAssertJ.*;

public class CatchExceptionsTest {

@Test

public void verifiesTypeAndMessage() {

when(new SomeClass()).someMethod();

then(caughtException())

.isInstanceOf(RuntimeException.class)

.hasMessage("Runtime exception occurred")

.hasMessageStartingWith("Runtime")

.hasMessageEndingWith("occured")

.hasMessageContaining("exception")

.hasNoCause();

}

}

看起来不错——简单,可读性高。没有JUnit运行。请注意,我指定的那个在SomeClass类中抛出异常的方法。可想而知,我可以在一个测试中检验多个异常。但是这违背了测试中的单一任务原则,所以不推荐这种做法。另外,如果你是用Eclipse工作的话,也许这篇文章对你有用。

检查异常的原因

我相信下面的代码就没有必要讨论了吧:

import org.junit.Test;

import static com.googlecode.catchexception.CatchException.*;

import static com.googlecode.catchexception.apis.CatchExceptionAssertJ.*;

public class CatchExceptionsTest {

@Test

public void verifiesCauseType() {

when(new ExceptionThrower()).someOtherMethod();

then(caughtException())

.isInstanceOf(RuntimeException.class)

.hasMessage("Runtime exception occurred")

.hasCauseExactlyInstanceOf(IllegalStateException.class)

.hasRootCauseExactlyInstanceOf(IllegalStateException.class);

}

}

使用Hamcrest来检查自定义异常

为了检查自定义异常,我用了在之前的文章中谈到的Hamcrest匹配代码。

class CustomException extends RuntimeException {

private final int code;

public CustomException(int code) {

this.code = code;

}

public int getCode() {

return code;

}

}

class ExceptionCodeMatches extends TypeSafeMatcher {

private int expectedCode;

public ExceptionCodeMatches(int expectedCode) {

this.expectedCode = expectedCode;

}

@Override

protected boolean matchesSafely(CustomException item) {

return item.getCode() == expectedCode;

}

@Override

public void describeTo(Description description) {

description.appendText("expects code ")

.appendValue(expectedCode);

}

@Override

protected void describeMismatchSafely(CustomException item, Description mismatchDescription) {

mismatchDescription.appendText("was ")

.appendValue(item.getCode());

}

}

测试部分:

import org.junit.Test;

import static com.googlecode.catchexception.CatchException.*;

import static org.junit.Assert.*;

public class CatchExceptionsTest {

@Test

public void verifiesCustomException() {

catchException(new ExceptionThrower(), CustomException.class).yetAnotherMethod(500);

assertThat((CustomException) caughtException(), new ExceptionCodeMatcher(500));

}

}

总结

catch-exception看起来很棒,上手非常简单。比起在JUnit中的方法它具有很多优势。如果有机会我竟会深入的研究一下这个库,希望在现实中能有一个这样的机会。

如果你感兴趣,可以看一下我其他的文章:

javacodegeeks 翻译:

ImportNew.com -

赖 信涛

译文链接:

http://www.importnew.com/10922.html

[

转载请保留原文出处、译者和译文链接。]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大学生参加学科竞赛有着诸多好处,不仅有助于个人综合素质的提升,还能为未来职业发展奠定良好基础。以下是一些分析: 首先,学科竞赛是提高专业知识和技能水平的有效途径。通过参与竞赛,学生不仅能够深入学习相关专业知识,还能够接触到最新的科研成果和技术发展趋势。这有助于拓展学生的学科视野,使其对专业领域有更深刻的理解。在竞赛过程,学生通常需要解决实际问题,这锻炼了他们独立思考和解决问题的能力。 其次,学科竞赛培养了学生的团队合作精神。许多竞赛项目需要团队协作来完成,这促使学生学会有效地与他人合作、协调分工。在团队合作,学生们能够学到如何有效沟通、共同制定目标和分工合作,这对于日后进入职场具有重要意义。 此外,学科竞赛是提高学生综合能力的一种途径。竞赛项目通常会涉及到理论知识、实际操作和创新思维等多个方面,要求参赛者具备全面的素质。在竞赛过程,学生不仅需要展现自己的专业知识,还需要具备创新意识和解决问题的能力。这种全面的综合能力培养对于未来从事各类职业都具有积极作用。 此外,学科竞赛可以为学生提供展示自我、树立信心的机会。通过比赛的舞台,学生有机会展现自己在专业领域的优势,得到他人的认可和赞誉。这对于培养学生的自信心和自我价值感非常重要,有助于他们更加积极主动地投入学习和未来的职业生涯。 最后,学科竞赛对于个人职业发展具有积极的助推作用。在竞赛脱颖而出的学生通常能够引起企业、研究机构等用人单位的关注。获得竞赛奖项不仅可以作为个人履历的亮点,还可以为进入理想的工作岗位提供有力的支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值