Testing Refactoring e Unit testing Tests get written(not postponed o Programmers get better at writing tests o Deeper understanding prior to coding Reusable automated verification e Rerun all unit tests after every code change o Change code w/o excessive debugging e JUnit is the testing framework for Java
Testing & Refactoring Unit testing ⚫ Tests get written (not postponed) ⚫ Programmers get better at writing tests ⚫ Deeper understanding prior to coding ⚫ Reusable automated verification ⚫ Rerun all unit tests after every code change ⚫ Change code w/o excessive debugging JUnit is the testing framework for Java
Testing Refactoring Unit testing steps Create object to be tested 2. Do something to it (invoke some method) 3. Check if results are correct Write tests for 1 method at a time
Testing & Refactoring Unit testing steps 1. Create object to be tested 2. Do something to it (invoke some method) 3. Check if results are correct Write tests for 1 method at a time
JUnit in Eclipse IDE Fig. 21.1 c Java- Test java- Eclipse Platform e Edit Source Refactor Navigate Search Pro 」公·西」」·0·9·0⑧·少」 nt2 Package Ex… Hierarch n DieTest java $ Finished after 0. 251 sends package test. com. craiglarman monopoly domain import com. craiglarman. monopoly domain x-l Runs: 1/1 E Errors: 0 2 Failures: 0 public class Die Test extends Testcase BFailuresElaHierarchy private Die fixture new Die() private static final int ROLLS MAX=100 l every roll should produce a face Value between 1 and 6 public void testRoll() for( int i=0; i< ROLLS MAX: 1++) fixture. roll(): int faceValue fixture. getFaceValue() assertTrue(( face Value>0)&&( faceValue <7)) E Failure Trace
JUnit in Eclipse IDE Fig. 21.1
Testing Refactoring Refactoring Structured, disciplined method of rewriting/restructuring code w/o changing its behavior Refactor in small steps Refactoring: Improving the design of Existing Code, by fowler Eclipse provides built-in support
Testing & Refactoring Refactoring ⚫ Structured, disciplined method of rewriting/restructuring code w/o changing its behavior ⚫ Refactor in small steps ⚫ ‘Refactoring: Improving the Design of Existing Code’, by Fowler ⚫ Eclipse provides built-in support
Testing Refactoring Why refactor? Improve design Remove duplicate code Improve clarity Shorten methods Remove hard-coded constants Move methods to another class See fowler's book for more Inseparable from iterative ooad
Testing & Refactoring Why refactor? Improve Design! ⚫ Remove duplicate code ⚫ Improve clarity ⚫ Shorten methods ⚫ Remove hard-coded constants ⚫ Move methods to another class ⚫ See Fowler’s book for more! ⚫ Inseparable from iterative OOAD