January 2016
Intermediate to advanced
168 pages
4h 6m
English
Keep the bar green to keep the code clean.
The jUnit motto
Automate tests for your codebase.
Do this by writing automated tests using a test framework.
This improves maintainability because automated testing makes development predictable and less risky.
In Chapter 4, we have presented isValid, a method to check whether bank account numbers comply with a checksum. That method contains a small algorithm that implements the checksum. It is easy to make mistakes in a method like this. That is why probably every programmer in the world at some point has written a little, one-off program to test the behavior of such a method, like so:
packageeu.sig.training.ch10;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importeu.sig.training.ch04.v1.Accounts;publicclassProgram{publicstaticvoidmain(String[]args)throwsIOException{BufferedReaderisr=newBufferedReader(newInputStreamReader(System.in));Stringacct;do{System.out.println("Type a bank account number on the next line.");acct=isr.readLine();System.out.println("Bank account number '"+acct+"' is"+(Accounts.isValid(acct)?"":" not")+" valid.");}while(acct!=null&&acct.length()!=0);}}
This is a Java class with a main method, so it can be run from the command line:
$ java Program Type a bank account number on the next line. 123456789 Bank account number '123456789' is valid. Type a bank account number ...