網頁

2014年1月6日 星期一

Effective Java單元測試TestNG

TestNG是另一種單元測試的framework,與JUnit的類似,這次Mix將使用它來撰寫測試程式,大部分所引用的class package都一樣,只差在JUnit與TestNG的字樣,可以直接用搜尋取代字串,快速替代掉,或是用IDE plugin直接轉換成測試程式碼。



它是為更廣泛的測試場合而設計的單元測試,可以運行在沒有修改過的JUnit測試,如果你用過JUnit,不妨試試它的功能,感受兩者之間的差異。




package org.openyu.java.role.vo.impl;

import org.testng.annotations.Test;
import org.testng.AssertJUnit;
import org.openyu.java.role.vo.Role;

/**
 * The Class RoleImplTest.
 */
public class RoleImplTestNG {

 /**
  * Instantiates a new role impl test.
  */
 public RoleImplTestNG() {
 }

 /**
  * Setter.
  */
 @Test
 public void setter() {
  Role result = new RoleImpl();
  result.setId(1);
  System.out.println(result.getId());
  AssertJUnit.assertEquals(1, result.getId());
  //
  result.setName("OpenYu");
  System.out.println(result.getName());
  AssertJUnit.assertEquals("OpenYu", result.getName());
  //
  result.setStrength(100);
  System.out.println(result.getStrength());
  AssertJUnit.assertEquals(100, result.getStrength());
 }

 /**
  * Clonez.
  */
 @Test
 public void clonez() {
  Role role = new RoleImpl();
  role.setId(1);
  role.setName("OpenYu");
  role.setStrength(100);

  // clone
  Role result = (Role) role.clone();
  AssertJUnit.assertNotNull(result);

  // 複製出來的role2與role1, 所有field值相等
  System.out.println(result.getId());
  AssertJUnit.assertEquals(role.getId(), result.getId());
  //
  System.out.println(result.getName());
  AssertJUnit.assertEquals(role.getName(), result.getName());
  //
  System.out.println(result.getStrength());
  AssertJUnit.assertEquals(role.getStrength(), result.getStrength());
  //
  result.setId(2);// role2改變id,不會改到role1.id
  System.out.println(result.getId());
  AssertJUnit.assertTrue(role.getId() != result.getId());
 }

 /**
  * Equals.
  */
 @Test
 public void equals() {
  Role role = new RoleImpl();
  role.setId(1);
  //
  Role role2 = new RoleImpl();
  role2.setId(1);
  //
  System.out.println(role.getId() + ", " + role2.getId());

  boolean result = role.equals(role2);
  System.out.println(result);
  // 尚未覆寫equals時,會測試失敗
  AssertJUnit.assertTrue(result);
 }
}






Mix的equals()依舊測試失敗了,與JUnit測試結果相同,只是呈現出來的畫面略有不同,至於要採用何者來作單元測試,並沒有強制規定,若你是第一次使用Unit Test,建議用JUnit就可以了,因為Eclipse Standard的版本,就已經幫你裝好了,若是想用TestNG,則需要另外安裝plugin,TestNG for Eclipse。




console查看其結果。

[TestNG] Running:
1
OpenYu
100
2
1, 1
false
1
OpenYu
100
PASSED: clonez
PASSED: setter
FAILED: equals
java.lang.AssertionError: 
 at org.testng.AssertJUnit.fail(AssertJUnit.java:59)
 at org.testng.AssertJUnit.assertTrue(AssertJUnit.java:24)
 at org.testng.AssertJUnit.assertTrue(AssertJUnit.java:33)
 at org.openyu.java.role.vo.impl.RoleImplTestNG.equals(RoleImplTestNG.java:82)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
 at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
 at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
 at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
 at org.testng.TestRunner.privateRun(TestRunner.java:767)
 at org.testng.TestRunner.run(TestRunner.java:617)
 at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
 at org.testng.SuiteRunner.run(SuiteRunner.java:240)
 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
 at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
 at org.testng.TestNG.run(TestNG.java:1057)
 at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
 at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
 at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)


===============================================
    Default test
    Tests run: 3, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Failures: 1, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@2c41d05d: 235 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@2fb3f8f6: 72 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@32ef2c60: 19 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@6fb000e7: 15 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@3f77b3cd: 21 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 15 ms












  1. 撰寫TestNG Test Case。
  2. 撰寫setter()測試方法
  3. 撰寫clonez()測試方法
  4. 撰寫setter()測試方法


沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...