Effective Unit Testing in Java with JUnit Framework
Learn the importance of unit testing, explore JUnit framework for testing, and discover best practices to validate your Java code efficiently. Dive into essential elements, test-driven development, and practical examples for effective testing.
Effective Unit Testing in Java with JUnit Framework
E N D
Presentation Transcript
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; } 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); } }
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
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); } }
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) ); } }
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) ); }
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
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 () { … }
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 () { … } }
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 }
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 }