1. @Test
在JUnit3,所有的test case的方法名都要以"test"为前缀prefix;
在JUnit4,在test case的方法前加上@Test,就明白了。
@Test
public void empty() {
/* test case 1*/
Collection collection = new ArrarList();
assertTrue(collection.isEmpty());
}
2. @Before 和 @After
@Before 和 @After 就是setUp()和tearDown()的替代。
@Before
public void runBeforeEveryTest() {
simpleMath = new SimpleMath();
}
@After
public void runAfterEveryTest() {
simpleMath = null;
}
3. @BeforeClass 和 @AfterClass
在JUnit3,如果想仅调用一次setUp()和tearDown() for all test cases, 使用TestSetup类;在JUnit4,就省事了:
@BeforeClass
public static void runBeforeClass() {
// run for one time before all test cases
}
@AfterClass
public static void runAfterClass() {
// run for one time after all test cases
}
4. 测试异常处理
在《JUnit3的使用》文章中,看到旧式的异常测试是在抛出异常的代码中放入 try 块,然后在 try 块的末尾加入一个 fail() 语句:
public void testCase2() {
/* test case 2*/
ArrayList emptyList = new ArrayList();
try {
Object o = emptyList.get(0);
fail("Should raise an IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException expected) {
assertTrue(true);
}
}
在JUnit4,添加@Test,使用参数“expected”,并指明抛出异常的Exception类:
@Test(expected = IndexOutOfBoundsException.class)
public void testCase2() {
/* test case 2*/
ArrayList emptyList = new ArrayList();
Object o = emptyList.get(0);
}
5. @Ignore
对于你想暂时不进行的test cse, 在该方法前添加@Ignore
@Ignore("Not Ready to Run")
@Test
public void multiplication() {
assertEquals(15, simpleMath.multiply(3, 5));
}
6. 设置超时
在@Test,使用"timeout"参数。如果测试运行的时间超过指定的毫秒数,则测试失败。
@Test(timeout=3000)
public void remoteBaseRelativeResolutionWithDirectory()
throws IOException, ParsingException {
readBuilder.parse("config.xml");
}
7.添加了新的断言
JUnit 4 为比较数组添加了两个 assert() 方法:
public static void assertEquals(Object[] expected, Object[] actual)
public static void assertEquals(String message, Object[] expected, Object[] actual)
这两个方法以最直接的方式比较数组:如果数组长度相同,且每个对应的元素相同,则两个数组相等,否则不相等。数组为空的情况也作了考虑。
@Test
public void listEquality() {
List<Integer> expected = new ArrayList<Integer>();
expected.add(5);
List<Integer> actual = new ArrayList<Integer>();
actual.add(5);
assertEquals(expected, actual);
}
8. JUnit4Adapter
为了能够在JUnit3环境下run JUnit4 test, 所以提供了JUnit4Adapter
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(SimpleMathTest.class);
}
9.其他
失败(assert 方法检测到的预期的错误)与错误(异常指出的非预期的错误)之间不再有任何差别。尽管 JUnit 3 测试运行程序仍然可以区别这些情况,而 JUnit 4 运行程序将不再能够区分。
转自博客:http://android.blog.51cto.com/268543/50979
分享到:
相关推荐
JUnit4
Junit4使用方法
运行junit4所需jar包,包括junit-4.11.jar与 hamcrest-core-1.3.jar
junit4教程(《Junit4初探》)
java junit4 jar完整包,junit可用来项目的单元测试。
junit4学习文档,有实例,非常详细,不懂的可以参考
这是一个junit4测试的jar包,copy进去就可用
该文档介绍了如何使用JUNIT4进行单元测试,给出了一个详细的例子
因 jdk5 中的新特性,JUnit4 也因此有了很大的改变。下面详细介绍 JUnit4 的使用方法,只是举个例子说明junit4 的使用
此文档 详细介绍了JUnit4 和 3的主要区别,进行了实例详解!可以作为面试备用资料!
在Eclipse中使用JUnit4进行单元测试!
eclipse使用和junit使用
本文介绍了JUnit 4并不是第一个提供基于注释的灵活模型的Java:trade_mark:测试框架。在修改JUnit之前很久TestNG 就已建立为一个基于注释的框架。事实上,是TestNG在Java编程中率先 实现了利用注释进行测试,这使它...
JUnit4基础文档
2.1 JUNITJUNIT JUNIT 3.X3.X3.X使用 .... .... .... ........... 3 2.1.1 下载与安装 下载与安装 .... .... .... ........ 3 2.1.2 使用方法 .... .... .... ............ 4 基本流程 (我们一般都是这么写) .....
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage,包含依赖包:junit-jupiter-5.5.1.jar,junit-jupiter-engine-5.5.1.jar,junit-...JUnit Vintage提供了一个在平台上运行JUnit 3和JUnit 4的TestEngine 。
如何在eclipse中使用junit4进行单体测试的说明教程
JUnit4概述,在Eclipse中使用JUnit4进行单元测试(初级篇、中级篇、高级篇)
JUnit4使用.pdf,JUnit就是一个可能帮助你在开发Java程序时进行测试的框架,我们将在这一节中尽可能深入地介绍JUnit的相关知识,但由于篇幅所限,本节过后还是有很多东西在等着你来探索,接住我的这块砖头,去发掘出...
3, 把你释放文件中的如:org.junit4_4.3.1文件拷贝Eclipse的\plugins目录下,如果你的电脑上的环境安装了MyEclipse6.0插件了,这个文件也就存在\plugins目录下了,也就是说你要写JUnit的测试类环境已经好了,可以...