1 / 12

Unit Testing

Unit Testing. Yonglei Tao. Test-First Development. Essential element in eXtreme Programming (XP) Test is written before the code Developers have to understand the specification thoroughly. A Simple Way for Unit Testing. class Point { int x, y; Point () { x = O; y = O; }

ccase
Download Presentation

Unit Testing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit Testing Yonglei Tao

  2. Test-First Development • Essential element in eXtreme Programming (XP) • Test is written before the code • Developers have to understand the specification thoroughly

  3. A Simple Way for Unit Testing class Point { int x, y; Point () { x = O; y = O; } Point ( int x, int y ) { this.x = x; this.y = y; } public String toString () { return ( "( " + x + ", " + y + " )" ); } public static void main(String[] args) { Point p1 = new Point(); System.out.println("(0,0)=“ + p1); Point p2 = new Point(3,5); System.out.println("(3,5)=" + p2); } }

  4. JUnit • A free framework for unit testing • Written by Kent Beck & Erich Gamma • Available at: http://www.junit.org/ • Helps create and run individual and groups of test cases • Allows test cases to be self-checked • Test cases are reusable • Run test cases whenever changes are made • Programmers become much less reluctant to improvetheir code since they can easily validate their changes

  5. An Example Class class Money {     private int amount;     private String currency;    public Money(int amt, String cur) {         amount= amt;         currency= cur;     }     public double getAmount() { return amount; } public String getCurrency() { return currency; } public Money add(Money m) {     return new Money(amount + m.amount, currency); } }

  6. Define a Test Case public class MoneyTest {   @Test public void testConstructor() {     Money m1= new Money(12, "USD");     Money m2= new Money(14, "USD");   assertTrue ( !m1.equals(null) );     assertEquals ( m1, m1 );     assertEquals ( m1, new Money(12, "USD") );     assertTrue ( !m1.equals(m2) ); } }

  7. More Test Method • Code a little, test a little, code a little, test a little @Test public void testAdd() {         Money m1= new Money(12, "USD");          Money m2= new Money(14, "USD");   Money expected= new Money(26, "USD");         Money result= m1.add(m2);            assertTrue ( expected.equals(result) );       }

  8. Assert Methods • assertTrue (Boolean cond) • assertTrue (String msg, Boolean cond) • assertFalse (Boolean cond) • assertFalse (String msg, Boolean cond) • assertEquals (Object expected, Object actual) • assertEquals (double expected, double actual, double delta) • assertNull (Object obj) • assertNotNull (Object obj) • assertNotSame (Object obj1, Object obj2) • And more to search for Java Class Assert

  9. Annotations public class MoneyTest {     private Money m1     private Money m2;   @Before  public void setUp() {         m1 = new Money(12, "USD");         m2 = new Money(14, "USD");     } @Ignore (“not ready yet”) public void testUndefinedMethod () { … }

  10. Annotations (Cont.) @Test public void testEquals() {     assertEquals ( m1, m1 );     assertEquals ( m1, new Money(12, "USD") );     assertTrue ( !m1.equals(m2) ); } @Test public void testAdd() {     Money expected= new Money(26, "USD");     Money result= m1.add(m2);     assertTrue ( expected.equals(result) ); } @Test (expected = ArithmeticException.class) public void testMethodwithBug () { … } }

  11. How to Test Class BankAccount? 1 /** 2 A bank account has a balance that can be changed by 3 deposits and withdrawals. 4 */ 5 publicclass BankAccount 6 { 7 privatedouble balance; 8 9 /** 10 Constructs a bank account with a zero balance. 11 */ 12 public BankAccount() 13 { 14 balance = 0; 15 } 16 17 /** 18 Constructs a bank account with a given balance. 19 @param initialBalance the initial balance 20 */ 21 public BankAccount(double initialBalance) 22 { 23 balance = initialBalance; 24 }

  12. 26 /** 27 Deposits money into the bank account. 28 @param amount the amount to deposit 29 */ 30 publicvoid deposit(double amount) 31 { 32 balance = balance + amount; 33 } 34 35 /** 36 Withdraws money from the bank account. 37 @param amount the amount to withdraw 38 */ 39 publicvoid withdraw(double amount) 40 { 41 balance = balance - amount; 42 } 43 44 /** 45 Gets the current balance of the bank account. 46 @return the current balance 47 */ 48 publicdouble getBalance() 49 { 50 return balance; 51 } 52 }

More Related