3.1 下载地址
https://github.com/KentBeck/junit/downloads
3.2 基础使用方法
如果有这样一个工具栏,用来进行字符串的转换,将按照数据库命名的习惯进行格式化 , 格式化后的数据为小写字母,并且使用下划线分割命名单词
public class WordDealUtil { public static String wordFormat4DB(String name){ if (name== null ) { return null ; } Pattern p = Pattern.compile ( "[A-Z]" ); Matcher m = p.matcher(name); StringBuffer sb = new StringBuffer(); while (m.find()){ if (m.start() != 0) { m.appendReplacement(sb, "_" + m.group()); } } return m.appendTail(sb).toString().toLowerCase(); } } |
然后定义一个测试类,对上面的方法进行测试,
注意:
1. 每一个测试的方法使用注解 org.junit.Test 修饰
2. 测试方法必须是 public void 修饰,且不可以带任何参数
3. 可以使用的测试判断方法在 Assert 里面有定义,主要是 assertEquals , assertFalse , assertTrue , assertSame( 用来比较两个对象的引用是否相等 ),assertNotSame(),assertNull, assertNotNull(),fail()( 用来抛出 AssertEorror 错误,用这个方法抛出异常进行模拟方法异常的情况 )
4. JUnitCore.main ( new String[]{ "mytool.WordDealUtilTest" }); 表示调用这个类里面的 Test 注解过的方法开始测试了。
5. 如果失败了,会有两种结果, failure 和 error
public class WordDealUtilTest { @Test public void wordFormat4DBNormal(){ String target = "employeeInfo" ; String result = WordDealUtil.wordFormat4DB (target); assertEquals ( "employee_info" ,result); }
@Test public void wordFormat4DBNull(){ String target = null ; String result = WordDealUtil.wordFormat4DB (target); assertNull (result); }
@Test public void wordFormat4DBEmpty(){ String target = "" ; String result = WordDealUtil.wordFormat4DB (target); assertEquals ( "" ,result ); }
@Test public void wordFormat4DB1(){ String target = "EmployeeInfo" ; String result = WordDealUtil.wordFormat4DB (target); assertEquals ( "employee_info" ,result); }
// 测试当尾字母为大写时的情况 @Test public void wordFormat4DBEnd(){ String target = "employeeInfoA" ; String result = WordDealUtil.wordFormat4DB (target); assertEquals ( "employee_info_a" , result); }
// 测试多个相连字母大写时的情况 @Test public void wordFormat4DBTogether(){ String target = "employeeAInfo" ; String result = WordDealUtil.wordFormat4DB (target); assertEquals ( "employee_a_info" , result); }
//JUnit 将测试失败的情况分为两种: failure 和 error 。 //Failure 一般由单元测试使用的断言方法判断失败引起,它表示在测试点发现了问题; // 而 error 则是由代码异常引起 public static void main(String[] a){ JUnitCore.main ( new String[]{ "mytool.WordDealUtilTest" }); } } |
3.3 Fixture
Fixture 是指在执行一个或者多个测试方法时需要的一系列公共资源或者数据,例如测试环境,测试数据等等。 JUnit 专门提供了设置公共 Fixture 的方法,同一测试类中的所有测试方法都可以共用它来初始化 Fixture 和注销 Fixture 。
使用一些注解,就可以在一些方法或者类之前或者之后进行一些操作,有点像 AOP 的感觉。。。
见下面的例子:
public class Example { @BeforeClass public static void init() { System.out.println("before class:init() ..."); }
@AfterClass public static void release() { System.out.println("after class:release() ..."); }
@Before public void before() { System.out.println(" 在方法执行前执行: before()..."); }
@After public void close() { System.out.println(" 方法结束后第一个执行 ,close().."); }
// 忽略测试 @Ignore("Not retry yet") public void testIndexOut() { System.out.println(" 被忽略的方法被加入测试方法序列,但是没有执行? Exception test..."); }
// 如果超时 1 秒钟就认为出错 , 并且可以捕获除以 0 的运算异常 @Test(timeout = 1000, expected = java.lang.ArithmeticException.class) public void testTimeout() { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("----------------- 过了 1 秒之后打印出来的! "); // 下面故意抛出一个运算异常 . System.out.println(1 / 0); }
// 测试捕获指定异常的测试用例 @Test(expected = java.lang.ArithmeticException.class) public void testEception() { System.out.println("----------------- 抛出异常之前 "); System.out.println(1 / 0); System.out.println(" 抛出异常之后 "); }
public Example() { System.out.println("Example 构造函数 ..."); }
public static void main(String[] args) { JUnitCore.main(new String[] { "mytool.Example" }); } /** JUnit version 4.9b2 before class:init() ... Example 构造函数 ... . 在方法执行前执行: before()... ----------------- 过了 1 秒之后打印出来的! 方法结束后第一个执行 ,close().. Example 构造函数 ... . 在方法执行前执行: before()... ----------------- 抛出异常之前 方法结束后第一个执行 ,close().. after class:release() ...
Time: 0.516
OK (2 tests) **/ } |
注意:
1. 上面还演示了两个很有用的 Test 的参数, timeout 和 expected 。前者表示指定时间之后没有出结果,就默认为失败;后者表示期望可以抛出指定的异常,如果不是这个异常(或者没有抛出异常)就会失败!注意,没有异常也是测试失败!!
2. 注意上面调用了两次构造函数 !
3. 忽略测试的注解: Ignore
4. 上面总共演示了这几种 Fixture 注解: Before,After, BeforeClass,AfterClass, Ignore,Test.
3.4 测试运行器,测试套件
JUnit 中所有的测试方法都是由它负责执行的。 JUnit 为单元测试提供了默认的测试运行器,但 JUnit 并没有限制必须使用默认的运行器。可以通过 RunWith 注解修改默认的测试运行器。
例如下面的,显示修改要在测试类上面使用的运行器:
@RunWith(CustomTestRunner.class) public class TestWordDealUtil { …… } |
随着项目进度的开展,单元测试类会越来越多,可是直到现在还只会一个一个的单独运行测试类,这在实际项目实践中肯定是不可行的。为了解决这个问题, JUnit 提供了一种批量运行测试类的方法 ,叫做测试套件
建立一个测试套件的例子如下:
/** * 一个测试套件的例子。 * 使用注解: RunWith :参数为 Suite 类 * SuiteClasses :参数为要放入测试套件的测试类组成的数组 , 可以有多个要测试的类! */ @RunWith(Suite.class) @Suite.SuiteClasses({ WordDealUtilTest.class}) public class MyTestSuit { public static void main(String[] a){ // 下面传入的参数是要调用的其他的测试的类,将会找那里的测试方法 . // 这里的 MyTestSuit 包含了其他的测试类 WordDealUtilTest ,要保证不要出现循环调用。 <span style="font-family: |