---
title: "junit几点"
author: "goofyan"
date: "2013-12-02T15:12:31Z"
tags: ["junit"]
categories: ["开源软件"]
slug: "junit"
draft: false
lastmod: "2013-12-02T15:12:31Z"
id: 1983801
url: "https://goofyan.iteye.com/blog/1983801"
---

junit.framework与org.juint.
1.对testcase，junit.framework是junit3的写法，org.unit是junit4的写法。
2.junit.framework。测试类继承自TestCase，函数名固定setUp()、tearDown()、testXXXX()....org.unit，@Before、@After、@Test
junit.framework，TestRunner类，主类。接受TestCase的类名为参数。两种方式，若有static suite()，执行全部test；否则执行所有"test"开头、不含参数的方法。 
3.将多个test添加成testsuite（testcase集合，testAll）的方式不同。

@Test，支持2类参数。如“expected=IndexOutOfBoundsException.class（表示运行某类exception）”和“timeout=100（表示若超过该时间则失败）”。其实是调用实现Test接口的expected()和timeout()方法

before在每个测试方法之前都会运行一次，public void；beforeclass在类中只运行一次，public static void，如连接数据库等。
After、Before等这些类中都包含元注解，
@Retention(RetentionPolicy.RUNTIME)注解会在class字节码文件中存在，在运行时可以通过反射获取到
@Target(ElementType.METHOD或ElementType.TYPE或ElementType.FIELD）表示@XXX修饰的是方法还是类还是字段或组合

执行结果三种：success、failure、exception

junit4.4引入assume、assertThat、Theory
assertThat，如assertThat(testedNumber, allOf(greaterThan(8), lessThan(16)));anyOf是匹配符（Hamcrest的匹配符，hamcrest-library-1.1.jar、hamcrest-core-1.1.jar org.hamcrest.Matchers）。
assume若假设成立，执行随后代码，否则不执行。可以使用 assumeThat 并配合hamcrest的匹配符Matcher
@Theory指定带参数的测试函数，不同于@Test不带参数。@Theory的参数来自数据集@DataPoint，如@DataPoint public static String str = "aaa";与@Theory的参数名没有关系。

以后有空仔细看看源代码
