9. Useful Libraries http://code.google.com/p/mockito/ Mockito is a mocking framework that tastes really good. It lets you write beautiful tests with clean & simple API. Mockito doesn't give you hangover because the tests are very readable and they produce clean verification errors.
14. Useful Libraries Put simply, Guice alleviates the need for factories and the use of new in your Java code. Think of Guice's @Inject as the new new http://code.google.com/p/google-guice/ google-guice
18. Useful Libraries The Guava project contains several of Google's core libraries that we rely on in our Java-based projects: collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, and so forth. guava-libraries http://code.google.com/p/guava-libraries/
19. Useful Libraries - guava-libraries Example 1: Objects public class Person { final String name, nickname; final Movie favMovie; @Override public boolean equals(Object object) { if (object instanceof Person) { Person that = ( Person ) object; return Objects .equal(this.name, that.name) && Objects .equal(this.nickname, that.nickname) && Objects .equal(this.favMovie, that.favMovie); } return false; } @Override public int hashCode() { return Objects .hashCode(name, nickname, favMovie); }
20. Useful Libraries - guava-libraries Example 2: Other useful things Preconditions .checkState(state == State .PLAYABLE, "Can't play movie; state is %s" , state); //will throw ISE with message … this.rating = Preconditio ns.checkNotNull(rating); //will throw NPE … //CharMatcher matcher = CharMatcher.is('x'); //CharMatcher matcher = CharMatcher.isNot('_'); //CharMatcher matcher = CharMatcher.oneOf("aeiou").negate(); CharMatcher matcher = CharMatcher .inRange('a', 'z').or(inRange('A', 'Z')); matcher.matchesAllOf(“some string”); matcher.matchesAnyOf(“other string”); … String s = Joiner.on(", ").join(episodesOnDisc); … StringBuilder sb = ...; Joiner .on("|").skipNulls().appendTo(sb, episodesOnDisc);
34. Does not require modifying projects or performing any other setup. EclEmma is a free Java code coverage tool for Eclipse http://www.eclemma.org/index.html http://emma.sourceforge.net/
35.
36. Powerful tool to organize your thoughts XMind is your friend! http://www.xmind.net/
46. Can have checks in build(), to ensure the result correctness.
47. Result can have no setters or modifiers – safe to work with.
48. Coding techniques Verbose unit tests @Test public void testProcess(){ … } @Test public void testRegister(){ … } …
49. Coding techniques Verbose unit tests @Test public void process Should FailOnNull(){…} @Test public void process Should WorkOnEmptyString(){…} @Test public void process Should CheckSemicolons(){…} … @Test public void register Should FailOnNull(){…} @Test public void register Should CallProcess (){…} … Friend of you the “ SHOULD ” is @Test public void testProcess(){ … } @Test public void testRegister(){ … } …
50. Coding techniques Do not write so many unit tests as they tell you “ Cargo cult programming is a style of computer programming that is characterized by the ritual inclusion of code or program structures that serve no real purpose. ” Wikipedia Wikipedia 100% code coverage
55. Coding techniques Avoid String-typing public class User { … public User(String login, String password, String home, String shell) { … } … } String login = … String password = … String home = … String shell = … User user = new User(login, password, home, shell); User user = new User(password, home, login, “ “);
56. Coding techniques Avoid String-typing public class User { … public User(Login login, Password password, Home home, Shell shell) { … } … } Login login = … Password password = … Home home = … Shell shell = … User user = new User(login, password, home, shell); User user = new User(password, home, login, “ “); // – will not compile Or use a builder pattern, keeping String as type of all these parameters