0
Testing and Testable code Paweł Szulc http://paulszulc.wordpress.com [email_address]
Testing and testable code <ul><li>Who am I? </li></ul><ul><ul><li>Paweł Szulc </li></ul></ul><ul><ul><ul><li>Former Sun Ca...
Java Developer (currently Wicket+Spring+JPA)
Agile Enthusiast </li></ul></ul></ul><ul><ul><li>Blog: http://paulszulc.wordpress.com
E-mail: paul.szulc@gmail.com </li></ul></ul>
Testing and testable code <ul><li>What are we going to talk about? </li></ul>
Testing and testable code <ul><li>What are we going to talk about? </li></ul><ul><ul><li>Test Driven Development </li></ul...
Testing and testable code <ul><li>What are we going to talk about? </li></ul><ul><ul><li>Test Driven Development
Testing </li></ul></ul>
Testing and testable code <ul><li>What are we going to talk about? </li></ul><ul><ul><li>Test Driven Development
Testing
Testable code </li></ul></ul>
Testing and testable code <ul><li>What are we going to talk about? </li></ul><ul><ul><li>Test Driven Development
Testing
Testable code </li></ul></ul><ul><li>So of course, we are going to talk about... </li></ul>
Testing and testable code <ul><li>What are we going to talk about? </li></ul><ul><ul><li>Test Driven Development
Testing
Testable code </li></ul></ul><ul><li>So of course, we are going to talk about... </li></ul>DESIGN
Testing and testable code <ul><li>What are we going to talk about? </li></ul><ul><ul><li>Test Driven Development
Testing
Testable code </li></ul></ul><ul><li>So of course, we are going to talk about... </li></ul>DESIGN controversial?
Testing and testable code <ul><li>The ultimate conference question </li></ul>
Testing and testable code <ul><li>The ultimate conference question </li></ul>
Testing and testable code <ul><li>The ultimate conference question </li></ul>
Testing and testable code <ul><li>The ultimate conference question </li></ul><ul><ul><li>Paradox? </li></ul></ul>
Testing and testable code <ul><li>The ultimate conference question </li></ul><ul><ul><li>Paradox?
After-party conclusion  </li></ul></ul><ul><ul><ul><li>TDD is good because I have large set of tests in my application
I don't use TDD because </li></ul></ul></ul><ul><ul><ul><ul><li>Code to complex
Code not maintainable – twice more effort </li></ul></ul></ul></ul>
Testing and testable code <ul><li>The ultimate conference question </li></ul><ul><ul><li>Paradox?
After-party conclusion  </li></ul></ul><ul><ul><ul><li>TDD is good because I have large set of tests in my application
I don't use TDD because </li></ul></ul></ul><ul><ul><ul><ul><li>Code to complex
Code not maintainable – twice more effort </li></ul></ul></ul></ul><ul><ul><li>Paradox? No. Misunderstanding? Yes! </li></...
Testing and testable code <ul><li>Test Driven Development </li></ul>
Testing and testable code <ul><li>Test Driven Development </li></ul><ul><ul><li>Really bad name </li></ul></ul>
Testing and testable code <ul><li>Test Driven Development </li></ul><ul><ul><li>Really bad name
It's not about testing, it's about requirements and design </li></ul></ul>
Testing and testable code <ul><li>Test Driven Development </li></ul><ul><ul><li>Really bad name
It's not about testing, it's about requirements and design
Large tests and relatively high coverage are just positive side effects </li></ul></ul>
Testing and testable code <ul><li>Test Driven Development </li></ul><ul><ul><li>Two pitfalls when doing TDD </li></ul></ul...
TDD Architect </li></ul></ul></ul>
Testing and testable code <ul><li>Test Driven Development </li></ul><ul><ul><li>Two pitfalls when doing TDD </li></ul></ul...
TDD Architect </li></ul></ul></ul>” Test Driven Development is like sex. If you don't like it, you probably ain't doing it...
Testing and testable code <ul><li>Example: </li></ul><ul><ul><li>Requirement: create login page </li></ul></ul>
Testing and testable code <ul><li>Example: </li></ul><ul><ul><li>Requirement: create login page </li></ul></ul><ul><ul><ul...
Should not login for incorrect login
Should not login for incorrect password
Not logged in user is an 'guest user'
Guest user has login 'Guest' </li></ul></ul></ul>
Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul>
Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul><ul><ul><li>Ok, let me see, what I got here... </li></...
Should not login fo... </li></ul></ul></ul>
Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul><ul><ul><li>Ok, let me see, what I got here... </li></...
Should not login fo... </li></ul></ul></ul><ul><ul><li>Ok, right so definitely what I need for start is ... </li></ul></ul>
Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul><ul><ul><li>Ok, let me see, what I got here... </li></...
Should not login fo... </li></ul></ul></ul><ul><ul><li>Ok, right so definitely what I need for start is ... </li></ul></ul...
Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul><ul><ul><li>Ok, let me see, what I got here... </li></...
Should not login fo... </li></ul></ul></ul><ul><ul><li>Ok, right so definitely what I need for start is ... </li></ul></ul...
User need to have login and password fields </li></ul></ul></ul>
Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul><ul><ul><li>Ok, let me see, what I got here... </li></...
Should not login fo... </li></ul></ul></ul><ul><ul><li>Ok, right so definitely what I need for start is ... </li></ul></ul...
User need to have login and password fields
Getters and setters! </li></ul></ul></ul>
Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul><ul><ul><li>Ok, let me see, what I got here... </li></...
Should not login fo... </li></ul></ul></ul><ul><ul><li>Ok, right so definitely what I need for start is ... </li></ul></ul...
User need to have login and password fields
Getters and setters!
Equals and hashCode methods for equality </li></ul></ul></ul>
Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul><ul><ul><li>Ok, let me see, what I got here... </li></...
Should not login fo... </li></ul></ul></ul><ul><ul><li>Ok, right so definitely what I need for start is ... </li></ul></ul...
User need to have login and password fields
Getters and setters!
Equals and hashCode methods for equality </li></ul></ul></ul><ul><ul><li>…  and so tests writing begins </li></ul></ul>
Testing and testable code @Test public void testUserCreation() throws Exception { User user = new User(); }
Testing and testable code @Test public void testUserHasLoginAndPassword() throws Exception { User user = new User(); user....
Testing and testable code @Test public void testEqualsMethodValidForSameLogin() throws Exception { User user1 = new User()...
Testing and testable code @Test public void testEqualsMethodReturnsFalseForDifferentLogin() throws    Exception { User use...
Testing and testable code @Test public void testEqualsHashCodeConstract() throws Exception { User user1 = new User(); user...
Testing and testable code public class User { private String login, password; public User() {  } public String getLogin() ...
Testing and testable code <ul><li>Example: </li></ul><ul><ul><li>Requirement: create login page </li></ul></ul><ul><ul><ul...
Should not login for incorrect login
Should not login for incorrect password
Not logged in user is an 'guest user'
Guest user has login 'Guest' </li></ul></ul></ul>
Testing and testable code <ul><li>Example: </li></ul><ul><ul><li>Requirement: create login page </li></ul></ul><ul><ul><ul...
Should not login for incorrect login
Should not login for incorrect password
Not logged in user is an 'guest user'
Guest user has login 'Guest' </li></ul></ul></ul>
Testing and testable code …  the test should really looked like this: @Test public void shouldLoginForCorrectLoginAndPassw...
Testing and testable code public class User { private String login, password; public User(String login,  String password) ...
Testing and testable code public class User { private String login, password; public User() {  } public String getLogin() ...
Testing and testable code <ul><li>Pitfall #1: TDD Prophet – consequences </li></ul><ul><ul><li>Code: </li></ul></ul><ul><u...
Smallest change in your code make a lot of tests fail, even if that change isn't caused be change of requirements
Some of the code might never be used. Ever! </li></ul></ul></ul><ul><ul><li>Design </li></ul></ul><ul><ul><ul><li>Not focu...
Created design based on experience only.
Design is complex, not maintainable </li></ul></ul></ul>
Testing and testable code <ul><li>Test Driven Design is about </li></ul>
Testing and testable code <ul><li>Test Driven Design is about </li></ul><ul><ul><li>Requirements </li></ul></ul><ul><ul><u...
Testing and testable code <ul><li>Test Driven Design is about </li></ul><ul><ul><li>Requirements </li></ul></ul><ul><ul><u...
Testing and testable code <ul><li>Just because you are writing tests before implementation, does not mean you are using TD...
Testing and testable code <ul><li>Just because you are writing tests before implementation, does not mean you are using TD...
Symptoms of being TDD Prophet: </li></ul><ul><ul><li>Testing getters and setters
Asking questions on Internet like: </li></ul></ul><ul><ul><ul><li>Should I test private methods?
Is 75% code coverage good enough? </li></ul></ul></ul>
Testing and testable code <ul><li>Pitfall #2: TDD Architect </li></ul>
Testing and testable code <ul><li>Pitfall #2: TDD Architect </li></ul><ul><ul><li>Gather all requirements </li></ul></ul>
Testing and testable code <ul><li>Pitfall #2: TDD Architect </li></ul><ul><ul><li>Gather all requirements
Create basic architecture using interfaces </li></ul></ul>
Testing and testable code <ul><li>Pitfall #2: TDD Architect </li></ul><ul><ul><li>Gather all requirements
Create basic architecture using interfaces
Start writing tests while implementing previously designed interfaces </li></ul></ul>
Testing and testable code <ul><li>Pitfall #2: TDD Architect - consequences </li></ul><ul><ul><li>Just complexed variation ...
Testing and testable code <ul><li>Pitfall #2: TDD Architect - consequences </li></ul><ul><ul><li>Just complexed variation ...
Bigger chances that code will do what client wants (requirements)
Will not stand a chance when the requirements change (design) </li></ul></ul>
Testing and testable code <ul><li>Pitfall #2: TDD Architect - consequences </li></ul><ul><ul><li>Just complexed variation ...
Bigger chances that code will do what client wants (requirements)
Will not stand a chance when the requirements change (design)
Again: not a TDD practice! </li></ul></ul>
Testing and testable code <ul><li>So what is TDD practice? </li></ul>
Testing and testable code <ul><li>So what is TDD practice – the idea: </li></ul><ul><ul><li>Add test
Run all tests and see the new one failing
Add some code
Run all tests and see the new one succeeds
Refactor </li></ul></ul>
Testing and testable code <ul><li>So what is TDD practice – the idea enhanced: </li></ul><ul><ul><li>Pick up a requirement
Add test
Run all tests and see the new one failing
Upcoming SlideShare
Loading in...5
×

Testing and Testable Code

3,364

Published on

About Test Driven Development, Testing and Testable Code. So really simply about... design

Published in: Technology, Education
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,364
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
146
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

Transcript of "Testing and Testable Code"

  1. 1. Testing and Testable code Paweł Szulc http://paulszulc.wordpress.com [email_address]
  2. 2. Testing and testable code <ul><li>Who am I? </li></ul><ul><ul><li>Paweł Szulc </li></ul></ul><ul><ul><ul><li>Former Sun Campus Ambassador
  3. 3. Java Developer (currently Wicket+Spring+JPA)
  4. 4. Agile Enthusiast </li></ul></ul></ul><ul><ul><li>Blog: http://paulszulc.wordpress.com
  5. 5. E-mail: paul.szulc@gmail.com </li></ul></ul>
  6. 6. Testing and testable code <ul><li>What are we going to talk about? </li></ul>
  7. 7. Testing and testable code <ul><li>What are we going to talk about? </li></ul><ul><ul><li>Test Driven Development </li></ul></ul>
  8. 8. Testing and testable code <ul><li>What are we going to talk about? </li></ul><ul><ul><li>Test Driven Development
  9. 9. Testing </li></ul></ul>
  10. 10. Testing and testable code <ul><li>What are we going to talk about? </li></ul><ul><ul><li>Test Driven Development
  11. 11. Testing
  12. 12. Testable code </li></ul></ul>
  13. 13. Testing and testable code <ul><li>What are we going to talk about? </li></ul><ul><ul><li>Test Driven Development
  14. 14. Testing
  15. 15. Testable code </li></ul></ul><ul><li>So of course, we are going to talk about... </li></ul>
  16. 16. Testing and testable code <ul><li>What are we going to talk about? </li></ul><ul><ul><li>Test Driven Development
  17. 17. Testing
  18. 18. Testable code </li></ul></ul><ul><li>So of course, we are going to talk about... </li></ul>DESIGN
  19. 19. Testing and testable code <ul><li>What are we going to talk about? </li></ul><ul><ul><li>Test Driven Development
  20. 20. Testing
  21. 21. Testable code </li></ul></ul><ul><li>So of course, we are going to talk about... </li></ul>DESIGN controversial?
  22. 22. Testing and testable code <ul><li>The ultimate conference question </li></ul>
  23. 23. Testing and testable code <ul><li>The ultimate conference question </li></ul>
  24. 24. Testing and testable code <ul><li>The ultimate conference question </li></ul>
  25. 25. Testing and testable code <ul><li>The ultimate conference question </li></ul><ul><ul><li>Paradox? </li></ul></ul>
  26. 26. Testing and testable code <ul><li>The ultimate conference question </li></ul><ul><ul><li>Paradox?
  27. 27. After-party conclusion </li></ul></ul><ul><ul><ul><li>TDD is good because I have large set of tests in my application
  28. 28. I don't use TDD because </li></ul></ul></ul><ul><ul><ul><ul><li>Code to complex
  29. 29. Code not maintainable – twice more effort </li></ul></ul></ul></ul>
  30. 30. Testing and testable code <ul><li>The ultimate conference question </li></ul><ul><ul><li>Paradox?
  31. 31. After-party conclusion </li></ul></ul><ul><ul><ul><li>TDD is good because I have large set of tests in my application
  32. 32. I don't use TDD because </li></ul></ul></ul><ul><ul><ul><ul><li>Code to complex
  33. 33. Code not maintainable – twice more effort </li></ul></ul></ul></ul><ul><ul><li>Paradox? No. Misunderstanding? Yes! </li></ul></ul>
  34. 34. Testing and testable code <ul><li>Test Driven Development </li></ul>
  35. 35. Testing and testable code <ul><li>Test Driven Development </li></ul><ul><ul><li>Really bad name </li></ul></ul>
  36. 36. Testing and testable code <ul><li>Test Driven Development </li></ul><ul><ul><li>Really bad name
  37. 37. It's not about testing, it's about requirements and design </li></ul></ul>
  38. 38. Testing and testable code <ul><li>Test Driven Development </li></ul><ul><ul><li>Really bad name
  39. 39. It's not about testing, it's about requirements and design
  40. 40. Large tests and relatively high coverage are just positive side effects </li></ul></ul>
  41. 41. Testing and testable code <ul><li>Test Driven Development </li></ul><ul><ul><li>Two pitfalls when doing TDD </li></ul></ul><ul><ul><ul><li>TDD Prophet
  42. 42. TDD Architect </li></ul></ul></ul>
  43. 43. Testing and testable code <ul><li>Test Driven Development </li></ul><ul><ul><li>Two pitfalls when doing TDD </li></ul></ul><ul><ul><ul><li>TDD Prophet
  44. 44. TDD Architect </li></ul></ul></ul>” Test Driven Development is like sex. If you don't like it, you probably ain't doing it right.”
  45. 45. Testing and testable code <ul><li>Example: </li></ul><ul><ul><li>Requirement: create login page </li></ul></ul>
  46. 46. Testing and testable code <ul><li>Example: </li></ul><ul><ul><li>Requirement: create login page </li></ul></ul><ul><ul><ul><li>Should log in for correct login and password
  47. 47. Should not login for incorrect login
  48. 48. Should not login for incorrect password
  49. 49. Not logged in user is an 'guest user'
  50. 50. Guest user has login 'Guest' </li></ul></ul></ul>
  51. 51. Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul>
  52. 52. Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul><ul><ul><li>Ok, let me see, what I got here... </li></ul></ul><ul><ul><ul><li>Should log in for correct login and pas..
  53. 53. Should not login fo... </li></ul></ul></ul>
  54. 54. Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul><ul><ul><li>Ok, let me see, what I got here... </li></ul></ul><ul><ul><ul><li>Should log in for correct login and pas..
  55. 55. Should not login fo... </li></ul></ul></ul><ul><ul><li>Ok, right so definitely what I need for start is ... </li></ul></ul>
  56. 56. Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul><ul><ul><li>Ok, let me see, what I got here... </li></ul></ul><ul><ul><ul><li>Should log in for correct login and pas..
  57. 57. Should not login fo... </li></ul></ul></ul><ul><ul><li>Ok, right so definitely what I need for start is ... </li></ul></ul><ul><ul><ul><li>I need User domain class! </li></ul></ul></ul>
  58. 58. Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul><ul><ul><li>Ok, let me see, what I got here... </li></ul></ul><ul><ul><ul><li>Should log in for correct login and pas..
  59. 59. Should not login fo... </li></ul></ul></ul><ul><ul><li>Ok, right so definitely what I need for start is ... </li></ul></ul><ul><ul><ul><li>I need User domain class!
  60. 60. User need to have login and password fields </li></ul></ul></ul>
  61. 61. Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul><ul><ul><li>Ok, let me see, what I got here... </li></ul></ul><ul><ul><ul><li>Should log in for correct login and pas..
  62. 62. Should not login fo... </li></ul></ul></ul><ul><ul><li>Ok, right so definitely what I need for start is ... </li></ul></ul><ul><ul><ul><li>I need User domain class!
  63. 63. User need to have login and password fields
  64. 64. Getters and setters! </li></ul></ul></ul>
  65. 65. Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul><ul><ul><li>Ok, let me see, what I got here... </li></ul></ul><ul><ul><ul><li>Should log in for correct login and pas..
  66. 66. Should not login fo... </li></ul></ul></ul><ul><ul><li>Ok, right so definitely what I need for start is ... </li></ul></ul><ul><ul><ul><li>I need User domain class!
  67. 67. User need to have login and password fields
  68. 68. Getters and setters!
  69. 69. Equals and hashCode methods for equality </li></ul></ul></ul>
  70. 70. Testing and testable code <ul><li>Pitfall #1: TDD Prophet </li></ul><ul><ul><li>Ok, let me see, what I got here... </li></ul></ul><ul><ul><ul><li>Should log in for correct login and pas..
  71. 71. Should not login fo... </li></ul></ul></ul><ul><ul><li>Ok, right so definitely what I need for start is ... </li></ul></ul><ul><ul><ul><li>I need User domain class!
  72. 72. User need to have login and password fields
  73. 73. Getters and setters!
  74. 74. Equals and hashCode methods for equality </li></ul></ul></ul><ul><ul><li>… and so tests writing begins </li></ul></ul>
  75. 75. Testing and testable code @Test public void testUserCreation() throws Exception { User user = new User(); }
  76. 76. Testing and testable code @Test public void testUserHasLoginAndPassword() throws Exception { User user = new User(); user.setLogin(&quot;login&quot;); user.setPassword(&quot;password&quot;); assertEquals(&quot;login&quot;, user.getLogin()); assertEquals(&quot;password&quot;, user.getPassword()); }
  77. 77. Testing and testable code @Test public void testEqualsMethodValidForSameLogin() throws Exception { User user1 = new User(); user1.setLogin(&quot;login&quot;); User user2 = new User(); user2.setLogin(&quot;login&quot;); assertEquals(user1, user2); }
  78. 78. Testing and testable code @Test public void testEqualsMethodReturnsFalseForDifferentLogin() throws Exception { User user1 = new User(); user1.setLogin(&quot;login&quot;); User user2 = new User(); user2.setLogin(&quot;login2&quot;); assertFalse(user1.equals(user2)); }
  79. 79. Testing and testable code @Test public void testEqualsHashCodeConstract() throws Exception { User user1 = new User(); user1.setLogin(&quot;login&quot;); User user2 = new User(); user2.setLogin(&quot;login&quot;); assertTrue(user1.equals(user2)); assertEquals(user1.hashCode(),user2.hashCode()); }
  80. 80. Testing and testable code public class User { private String login, password; public User() { } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (!login.equals(user.login)) return false; return true; } public int hashCode() { return login.hashCode(); } } Design you get:
  81. 81. Testing and testable code <ul><li>Example: </li></ul><ul><ul><li>Requirement: create login page </li></ul></ul><ul><ul><ul><li>Should log in for correct login and password
  82. 82. Should not login for incorrect login
  83. 83. Should not login for incorrect password
  84. 84. Not logged in user is an 'guest user'
  85. 85. Guest user has login 'Guest' </li></ul></ul></ul>
  86. 86. Testing and testable code <ul><li>Example: </li></ul><ul><ul><li>Requirement: create login page </li></ul></ul><ul><ul><ul><li>Should log in for correct login and password
  87. 87. Should not login for incorrect login
  88. 88. Should not login for incorrect password
  89. 89. Not logged in user is an 'guest user'
  90. 90. Guest user has login 'Guest' </li></ul></ul></ul>
  91. 91. Testing and testable code … the test should really looked like this: @Test public void shouldLoginForCorrectLoginAndPassword() throws Exception{ // given String login = &quot;login&quot;; String password = &quot;password&quot;; dao.persist(new User(login,password)); // when User user = service.logIn(login, password); // then assertEquals(login, user.getLogin()); assertEquals(password, user.getPassword()); }
  92. 92. Testing and testable code public class User { private String login, password; public User(String login, String password) { this.login = login; this.password = password; } public String getLogin() { return login; } public String getPassword() { return password; } } Design you get:
  93. 93. Testing and testable code public class User { private String login, password; public User() { } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (!login.equals(user.login)) return false; return true; } public int hashCode() { return login.hashCode(); } } public class User { private String login, password; public User(String login, String password) { this.login = login; this.password = password; } public String getLogin() { return login; } public String getPassword() { return password; } }
  94. 94. Testing and testable code <ul><li>Pitfall #1: TDD Prophet – consequences </li></ul><ul><ul><li>Code: </li></ul></ul><ul><ul><ul><li>You are stuck with huge test base and an implementation that you might never need
  95. 95. Smallest change in your code make a lot of tests fail, even if that change isn't caused be change of requirements
  96. 96. Some of the code might never be used. Ever! </li></ul></ul></ul><ul><ul><li>Design </li></ul></ul><ul><ul><ul><li>Not focusing on requirements
  97. 97. Created design based on experience only.
  98. 98. Design is complex, not maintainable </li></ul></ul></ul>
  99. 99. Testing and testable code <ul><li>Test Driven Design is about </li></ul>
  100. 100. Testing and testable code <ul><li>Test Driven Design is about </li></ul><ul><ul><li>Requirements </li></ul></ul><ul><ul><ul><li>Implement only what the client wants </li></ul></ul></ul>
  101. 101. Testing and testable code <ul><li>Test Driven Design is about </li></ul><ul><ul><li>Requirements </li></ul></ul><ul><ul><ul><li>Implement only what the client wants </li></ul></ul></ul><ul><ul><li>Design </li></ul></ul><ul><ul><ul><li>Smallest design that does the job right </li></ul></ul></ul>
  102. 102. Testing and testable code <ul><li>Just because you are writing tests before implementation, does not mean you are using TDD practice. </li></ul>
  103. 103. Testing and testable code <ul><li>Just because you are writing tests before implementation, does not mean you are using TDD practice.
  104. 104. Symptoms of being TDD Prophet: </li></ul><ul><ul><li>Testing getters and setters
  105. 105. Asking questions on Internet like: </li></ul></ul><ul><ul><ul><li>Should I test private methods?
  106. 106. Is 75% code coverage good enough? </li></ul></ul></ul>
  107. 107. Testing and testable code <ul><li>Pitfall #2: TDD Architect </li></ul>
  108. 108. Testing and testable code <ul><li>Pitfall #2: TDD Architect </li></ul><ul><ul><li>Gather all requirements </li></ul></ul>
  109. 109. Testing and testable code <ul><li>Pitfall #2: TDD Architect </li></ul><ul><ul><li>Gather all requirements
  110. 110. Create basic architecture using interfaces </li></ul></ul>
  111. 111. Testing and testable code <ul><li>Pitfall #2: TDD Architect </li></ul><ul><ul><li>Gather all requirements
  112. 112. Create basic architecture using interfaces
  113. 113. Start writing tests while implementing previously designed interfaces </li></ul></ul>
  114. 114. Testing and testable code <ul><li>Pitfall #2: TDD Architect - consequences </li></ul><ul><ul><li>Just complexed variation of TDD Prophet </li></ul></ul>
  115. 115. Testing and testable code <ul><li>Pitfall #2: TDD Architect - consequences </li></ul><ul><ul><li>Just complexed variation of TDD Prophet
  116. 116. Bigger chances that code will do what client wants (requirements)
  117. 117. Will not stand a chance when the requirements change (design) </li></ul></ul>
  118. 118. Testing and testable code <ul><li>Pitfall #2: TDD Architect - consequences </li></ul><ul><ul><li>Just complexed variation of TDD Prophet
  119. 119. Bigger chances that code will do what client wants (requirements)
  120. 120. Will not stand a chance when the requirements change (design)
  121. 121. Again: not a TDD practice! </li></ul></ul>
  122. 122. Testing and testable code <ul><li>So what is TDD practice? </li></ul>
  123. 123. Testing and testable code <ul><li>So what is TDD practice – the idea: </li></ul><ul><ul><li>Add test
  124. 124. Run all tests and see the new one failing
  125. 125. Add some code
  126. 126. Run all tests and see the new one succeeds
  127. 127. Refactor </li></ul></ul>
  128. 128. Testing and testable code <ul><li>So what is TDD practice – the idea enhanced: </li></ul><ul><ul><li>Pick up a requirement
  129. 129. Add test
  130. 130. Run all tests and see the new one failing
  131. 131. Add some code
  132. 132. Run all tests and see the new one succeeds
  133. 133. Refactor </li></ul></ul>
  134. 134. Testing and testable code <ul><li>So what is TDD practice – the idea enhanced: </li></ul><ul><ul><li>Pick up a requirement
  135. 135. Add test that represents the requirement
  136. 136. Run all tests and see the new one failing
  137. 137. Add some code
  138. 138. Run all tests and see the new one succeeds
  139. 139. Refactor </li></ul></ul>
  140. 140. Testing and testable code <ul><li>So what is TDD practice – the idea enhanced: </li></ul><ul><ul><li>Pick up a requirement
  141. 141. Add test that represents the requirement
  142. 142. Run all tests and see the new one failing
  143. 143. Add some code
  144. 144. Run all tests and see the new one succeeds
  145. 145. Refactor whenever design is getting ugly </li></ul></ul>
  146. 146. Testing and testable code <ul><li>So what is TDD practice – the idea enhanced: </li></ul><ul><ul><li>Pick up a requirement
  147. 147. Add test that represents the requirement
  148. 148. Run all tests and see the new one failing
  149. 149. Add some code
  150. 150. Run all tests and see the new one succeeds
  151. 151. Refactor whenever design is getting ugly </li></ul></ul><ul><li>You start with requirement, you end up with design! </li></ul>
  152. 152. Testing and testable code <ul><li>Test Driven Development </li></ul><ul><ul><li>Really bad name
  153. 153. It's not about testing, it's about requirements and design
  154. 154. Large tests and relatively high coverage are just positive side effects </li></ul></ul>
  155. 155. Testing and testable code <ul><li>Test Driven Development </li></ul><ul><ul><li>Really bad name </li></ul></ul><ul><ul><ul><li>Test Driven Design
  156. 156. Requirements Driven Design </li></ul></ul></ul><ul><ul><li>It's not about testing, it's about requirements and design
  157. 157. Large tests and relatively high coverage are just positive side effects </li></ul></ul>
  158. 158. Testing and testable code <ul><li>Test Driven Development </li></ul><ul><ul><li>Really bad name </li></ul></ul><ul><ul><ul><li>Test Driven Design
  159. 159. Requirements Driven Design </li></ul></ul></ul><ul><ul><li>It's not about testing, it's about requirements and design </li></ul></ul><ul><ul><ul><li>Tests are simply translation of requirements into code language </li></ul></ul></ul><ul><ul><li>Large tests and relatively high coverage are just positive side effects </li></ul></ul>
  160. 160. Testing and testable code The BIG Problem...
  161. 161. Testing and testable code The BIG Problem... You still simply ain't gonna use TDD.
  162. 162. Testing and testable code The BIG Problem... You still simply ain't gonna use TDD. … whatever the reasons are.
  163. 163. Testing and testable code <ul><li>The Big Problem </li></ul><ul><ul><li>” Test Driven Development is like riding a bike” </li></ul></ul>– Misko Hevery,GeeCON2009
  164. 164. Testing and testable code <ul><li>The Big Problem </li></ul><ul><ul><li>” Test Driven Development is like riding a bike” </li></ul></ul>– Misko Hevery,GeeCON2009 <ul><li>How to start riding that bike? </li></ul>
  165. 165. Testing and testable code <ul><li>The Big Problem </li></ul><ul><ul><li>” Test Driven Development is like riding a bike” </li></ul></ul>– Misko Hevery,GeeCON2009 <ul><li>How to start riding that bike? </li></ul><ul><ul><li>TDD small app </li></ul></ul>
  166. 166. Testing and testable code <ul><li>The Big Problem </li></ul><ul><ul><li>” Test Driven Development is like riding a bike” </li></ul></ul>– Misko Hevery,GeeCON2009 <ul><li>How to start riding that bike? </li></ul><ul><ul><li>TDD small app
  167. 167. Bugs Driven Tests </li></ul></ul>
  168. 168. Testing and testable code <ul><li>Bugs Driven Tests </li></ul>
  169. 169. Testing and testable code <ul><li>Bugs Driven Tests </li></ul><ul><ul><li>Pick up a bug </li></ul></ul>
  170. 170. Testing and testable code <ul><li>Bugs Driven Tests </li></ul><ul><ul><li>Pick up a bug
  171. 171. Write test that checks whether the bug is fixed </li></ul></ul>
  172. 172. Testing and testable code <ul><li>Bugs Driven Tests </li></ul><ul><ul><li>Pick up a bug
  173. 173. Write test that checks whether th bug is fixed
  174. 174. Test should fail </li></ul></ul>
  175. 175. Testing and testable code <ul><li>Bugs Driven Tests </li></ul><ul><ul><li>Pick up a bug
  176. 176. Write test that checks whether th bug is fixed
  177. 177. Test should fail
  178. 178. Fix the bug </li></ul></ul>
  179. 179. Testing and testable code <ul><li>Bugs Driven Tests </li></ul><ul><ul><li>Pick up a bug
  180. 180. Write test that checks whether th bug is fixed
  181. 181. Test should fail
  182. 182. Fix the bug
  183. 183. Test should succeed </li></ul></ul>
  184. 184. Testing and testable code <ul><li>Bugs Driven Tests – pros </li></ul><ul><ul><li>Obvious </li></ul></ul><ul><ul><ul><li>You know your bug is fixed when your test passes.
  185. 185. No bugs regression </li></ul></ul></ul>
  186. 186. Testing and testable code <ul><li>Bugs Driven Tests – pros </li></ul><ul><ul><li>Obvious </li></ul></ul><ul><ul><ul><li>You know your bug is fixed when your test passes.
  187. 187. No bugs regression </li></ul></ul></ul><ul><ul><li>Cognitive </li></ul></ul><ul><ul><ul><li>You start writing tests. It becomes something natural.
  188. 188. Rule 80:20 </li></ul></ul></ul><ul><ul><ul><ul><li>20% code tested
  189. 189. 80% of importance </li></ul></ul></ul></ul>
  190. 190. Testing and testable code <ul><li>Bugs Driven Tests – cons </li></ul><ul><ul><li>I just can't! </li></ul></ul><ul><ul><ul><li>” I would really like to write test for this implementation. But I just simply don't know how!” </li></ul></ul></ul>
  191. 191. Testing and testable code <ul><li>Bugs Driven Tests – cons </li></ul><ul><ul><li>I just can't! </li></ul></ul><ul><ul><ul><li>” I would really like to write test for this implementation. But I just simply don't know how!” </li></ul></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><li>Library – the ultimate University example :) </li></ul></ul></ul>
  192. 192. Testing and testable code <ul><li>Bugs Driven Tests – cons </li></ul>public class Library { private BookSearch bookSearch; public Library() { bookSearch = new BookSearch(); } public boolean addReader(Reader r) { ... } }
  193. 193. Testing and testable code <ul><li>Bugs Driven Tests – cons </li></ul>public class Library { private BookSearch bookSearch; public Library() { bookSearch = new BookSearch(); } public boolean addReader(Reader r) { ... } }
  194. 194. Testing and testable code <ul><li>Bugs Driven Tests – cons </li></ul>public class BookSearch { public BookSearch() { DBConnection.init(); } }
  195. 195. Testing and testable code <ul><li>Bugs Driven Tests – cons </li></ul><ul><ul><li>Problem, because you write your test AFTER the implementation
  196. 196. Code might be not testable and so writing tests hard or even impossible </li></ul></ul>
  197. 197. Testing and testable code <ul><li>Testable code </li></ul>
  198. 198. Testing and testable code <ul><li>Testable code </li></ul><ul><ul><li>Misko Hevery ( http://misko.hevery.com ) </li></ul></ul>
  199. 199. Testing and testable code <ul><li>Testable code </li></ul><ul><ul><li>Misko Hevery ( http://misko.hevery.com ) </li></ul></ul><ul><li>Misko Hevery and gruppies </li></ul>
  200. 200. Testing and testable code <ul><li>Testable code </li></ul>
  201. 201. Testing and testable code <ul><li>Testable code </li></ul><ul><ul><li>Makes testing light and simple activity </li></ul></ul>
  202. 202. Testing and testable code <ul><li>Testable code </li></ul><ul><ul><li>Makes testing light and simple activity
  203. 203. Promotes good design and good quality code </li></ul></ul>
  204. 204. Testing and testable code <ul><li>Testable code </li></ul><ul><ul><li>Makes testing light and simple activity
  205. 205. Promotes good design and good quality code
  206. 206. Declarative </li></ul></ul>
  207. 207. Testing and testable code <ul><li>Testable code </li></ul><ul><ul><li>Makes testing light and simple activity
  208. 208. Promotes good design and good quality code
  209. 209. Declarative
  210. 210. Easy to read </li></ul></ul>
  211. 211. Testing and testable code <ul><li>Testable code </li></ul><ul><ul><li>Makes testing light and simple activity
  212. 212. Promotes good design and good quality code
  213. 213. Declarative
  214. 214. Easy to read
  215. 215. Easy to maintain </li></ul></ul>
  216. 216. Testing and testable code <ul><li>Global state </li></ul><ul><ul><li>Good or Bad? </li></ul></ul>
  217. 217. Testing and testable code <ul><li>Global state </li></ul>int a = new Foo().bar(); int b = new Foo().bar(); Does: a == b or a !=b
  218. 218. Testing and testable code <ul><li>Global state </li></ul>Insanity noun ” Repeating the same thing and expecting a different result.”
  219. 219. Testing and testable code <ul><li>Global state </li></ul>
  220. 220. Testing and testable code <ul><li>Global state </li></ul>
  221. 221. Testing and testable code <ul><li>Global state </li></ul>
  222. 222. Testing and testable code <ul><li>Global state </li></ul>
  223. 223. Testing and testable code <ul><li>Global state – root problem: globally accessible state </li></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><li>Class A and class B
  224. 224. A and B does not know anything about each other
  225. 225. Both states depends on global variable </li></ul></ul></ul><ul><ul><ul><ul><li>static int count; </li></ul></ul></ul></ul>
  226. 226. Testing and testable code <ul><li>Global state – root problem: globally accessible state </li></ul><ul><ul><li>Why problem? </li></ul></ul><ul><ul><ul><li>Developers need to read every single line of code to understand the implementation
  227. 227. Test are not isolated, even if they look like they are </li></ul></ul></ul>” You may not have thought of it this way before, but whenever you use static state, you’re creating secret communication channels and not making them clear in the API.”
  228. 228. Testing and testable code <ul><li>Global state – root problem: globally accessible state </li></ul><ul><ul><li>Why problem? </li></ul></ul><ul><ul><ul><li>Every test using global state needs it to start in an expected state, or the test will fail.
  229. 229. Global state often prevents tests from being able to run in parallel </li></ul></ul></ul>
  230. 230. Testing and testable code <ul><li>Singletons </li></ul><ul><ul><li>Good or bad? </li></ul></ul>
  231. 231. Testing and testable code <ul><li>Singletons </li></ul><ul><ul><li>Singletons are global state! </li></ul></ul><ul><ul><ul><li>Gang Of Four singletons </li></ul></ul></ul><ul><ul><ul><ul><li>Private constructor
  232. 232. Static method instantiating </li></ul></ul></ul></ul>
  233. 233. Testing and testable code <ul><li>Singletons </li></ul><ul><ul><li>Singletons are global state! </li></ul></ul><ul><ul><ul><li>Gang Of Four singletons </li></ul></ul></ul><ul><ul><ul><ul><li>Private constructor
  234. 234. Static method instantiating </li></ul></ul></ul></ul><ul><ul><li>Same situation: class A and B both hold reference to singleton C affect each other state </li></ul></ul>
  235. 235. Testing and testable code <ul><li>Singletons </li></ul><ul><ul><li>Singletons are global state! </li></ul></ul><ul><ul><ul><li>Gang Of Four singletons </li></ul></ul></ul><ul><ul><ul><ul><li>Private constructor
  236. 236. Static method instantiating </li></ul></ul></ul></ul><ul><ul><li>Same situation: class A and B both hold reference to singleton C affect each other state
  237. 237. Only exception: no state (rarely happens) </li></ul></ul>
  238. 238. Testing and testable code <ul><li>Singletons </li></ul><ul><ul><li>Singletons are global state! </li></ul></ul><ul><ul><ul><li>Gang Of Four singletons </li></ul></ul></ul><ul><ul><ul><ul><li>Private constructor
  239. 239. Static method instantiating </li></ul></ul></ul></ul><ul><ul><li>Same situation: class A and B both hold reference to singleton C affect each other state
  240. 240. Only exception: no state (rarely happens)
  241. 241. Tests: methods like reset() </li></ul></ul>
  242. 242. Testing and testable code <ul><li>Main problem with global states (inclugin singletons) </li></ul><ul><ul><li>They are liars </li></ul></ul>
  243. 243. Testing and testable code <ul><li>Main problem with global states (inclugin singletons) </li></ul><ul><ul><li>They are liars
  244. 244. They hide from developers true intentions of the implementation </li></ul></ul>
  245. 245. Testing and testable code <ul><li>Main problem with global states (inclugin singletons) </li></ul><ul><ul><li>They are liars
  246. 246. They hide from developers true intentions of the implementation
  247. 247. Far from ”clean code that talks” </li></ul></ul>
  248. 248. Testing and testable code <ul><li>Dependency Injection </li></ul><ul><ul><li>Good or bad? </li></ul></ul>
  249. 249. Testing and testable code <ul><li>Dependency Injection </li></ul><ul><ul><li>Library example once again </li></ul></ul>
  250. 250. Testing and testable code <ul><li>Dependency Injection </li></ul><ul><ul><li>Library example once again
  251. 251. Common flaw: constructor does real work </li></ul></ul><ul><ul><ul><li>creating/initializing collaborators
  252. 252. communicating with other services
  253. 253. logic to set up its own state </li></ul></ul></ul>
  254. 254. Testing and testable code <ul><li>Dependency Injection </li></ul><ul><ul><li>Library example once again
  255. 255. Common flaw: constructor does real work </li></ul></ul><ul><ul><ul><li>creating/initializing collaborators
  256. 256. communicating with other services
  257. 257. logic to set up its own state </li></ul></ul></ul><ul><ul><li>Why problem? </li></ul></ul><ul><ul><ul><li>Inflexible coupled design
  258. 258. Unable to inject test mocks
  259. 259. It force collaboration on you </li></ul></ul></ul><ul><ul><ul><ul><li>Object graph
  260. 260. Collaboration graph </li></ul></ul></ul></ul>
  261. 261. Testing and testable code <ul><li>Dependency Injection </li></ul><ul><ul><li>Library example </li></ul></ul>public class Library { private BookSearch bookSearch; public Library() { this.bookSearch = new BookSearch(); } public boolean addReader(Reader r) { ... } }
  262. 262. Testing and testable code <ul><li>Dependency Injection </li></ul><ul><ul><li>Library example </li></ul></ul>public class Library { private BookSearch bookSearch; @Inject public Library(BookSearch bookSearch) { this.bookSearch = bookSearch; } public boolean addReader(Reader r) { ... } }
  263. 263. Testing and testable code <ul><li>Using Dependency Injection </li></ul><ul><ul><li>You end up with loosely coupled design </li></ul></ul>
  264. 264. Testing and testable code <ul><li>Using Dependency Injection </li></ul><ul><ul><li>You end up with loosely coupled design
  265. 265. Easy to understand classes </li></ul></ul><ul><ul><ul><li>Object graph vs creation graph
  266. 266. Declarative style of programming </li></ul></ul></ul>
  267. 267. Testing and testable code <ul><li>Using Dependency Injection </li></ul><ul><ul><li>You end up with loosely coupled design
  268. 268. Easy to understand classes </li></ul></ul><ul><ul><ul><li>Object graph vs creation graph
  269. 269. Declarative style of programming </li></ul></ul></ul><ul><ul><li>Tests are easy to write </li></ul></ul>
  270. 270. Testing and testable code <ul><li>More? Give us more! </li></ul><ul><ul><li>I would, simply no time... I think?
  271. 271. http://misko.hevery.com/code-reviewers-guide/ </li></ul></ul>
  272. 272. Testing and testable code <ul><li>Small summery: </li></ul>
  273. 273. Testing and testable code <ul><li>Small summery: </li></ul><ul><ul><li>Just because you write test first, does not mean you are using TDD </li></ul></ul>
  274. 274. Testing and testable code <ul><li>Small summery: </li></ul><ul><ul><li>Just because you write test first, does not mean you are using TDD
  275. 275. TDD is about design and requirements </li></ul></ul>
  276. 276. Testing and testable code <ul><li>Small summery: </li></ul><ul><ul><li>Just because you write test first, does not mean you are using TDD
  277. 277. TDD is about design and requirements
  278. 278. More often you use TDD more addicting it becomes </li></ul></ul>
  279. 279. Testing and testable code <ul><li>Small summery: </li></ul><ul><ul><li>Just because you write test first, does not mean you are using TDD
  280. 280. TDD is about design and requirements
  281. 281. More often you use TDD more addicting it becomes
  282. 282. If used efficiently, you develop your code with good pace, code is readable and maintainable. You rarely hear about regression bugs. </li></ul></ul>
  283. 283. Testing and testable code <ul><li>Small summery: </li></ul><ul><ul><li>Just because you write test first, does not mean you are using TDD
  284. 284. TDD is about design and requirements
  285. 285. More often you use TDD more addicting it becomes
  286. 286. If used efficiently, you develop your code with good pace, code is readable and maintainable. You rarely hear about regression bugs.
  287. 287. Bugs Driven Testing – good starting point </li></ul></ul>
  288. 288. Testing and testable code <ul><li>Small summery: </li></ul><ul><ul><li>Just because you write test first, does not mean you are using TDD
  289. 289. TDD is about design and requirements
  290. 290. More often you use TDD more addicting it becomes
  291. 291. If used efficiently, you develop your code with good pace, code is readable and maintainable. You rarely hear about regression bugs.
  292. 292. Bugs Driven Testing – good starting point
  293. 293. Testable code: clean code that talks </li></ul></ul>
  294. 294. Testing and testable code Q&A
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×