2014 GeeCON Custom Assertions

978 views

Published on

my GeeCON 2014 "lightning talk" about custom assertions

Published in: Software, Technology
  • Be the first to comment

2014 GeeCON Custom Assertions

  1. 1. Custom Assertions ...when assertEquals() is not enough Tomek Kaczanowski
  2. 2. freegreatpicture.com A 15 minutes „lightning talk”
  3. 3. ● Developer ● Blogger ● Books author twitter: @tkaczanowski http://practicalunittesting.com Tomek Kaczanowski
  4. 4. Expectations our tests should: ● verify americanmethod.com
  5. 5. Expectations our tests should: ● verify ● be readable code.tutsplus.com
  6. 6. Expectations our tests should: ● verify ● tell a story jasonfurnell.wordpress.com
  7. 7. Expectations our tests should: ● verify ● tell a story ● be focused noble.org
  8. 8. Expectations our assertions should: ● verify ● tell a story ● be focused
  9. 9. The Evolution of Assertions
  10. 10. The Evolution of Assertions
  11. 11. The Evolution of Assertions
  12. 12. The Evolution of Assertions
  13. 13. The Evolution of Assertions
  14. 14. Talk is cheap. Show me the code.
  15. 15. assertThat(...)
  16. 16. assertThat(...)
  17. 17. assertThat(...)
  18. 18. The Anatomy of Custom Assertion
  19. 19. The Anatomy of Custom Assertion
  20. 20. The Anatomy of Custom Assertion
  21. 21. Custom Assertions or Private Methods? verifySuccessfulRedirect(response, FALLBACK_URL); dashboardShows(dashboardPage, „my campaign”, „Target”, „PL”); noHits(„2007-06-06 02:00”); noHits(„2007-06-06 03:00”); hitsAndRevenue(„2007-06-06 04:00”, 2, 7); ...
  22. 22. Real Life Examples
  23. 23. Real Life Examples actual
  24. 24. Real Life Examples actual assertThat(found) .overwritingErrorMessage( „expected row for {} but found only rows for {}” stringDate, dates(actual) ) .isTrue();
  25. 25. Real Life Examples actual
  26. 26. Conclusions
  27. 27. Upsides ● readable ● reusable ● simple to write ● cheap to maintain
  28. 28. Upsides and Downsides ● need to write them ● can hide bad smells ● readable ● reusable ● simple to write ● cheap to maintain
  29. 29. Best Practices use AssertJ • forget about JUnit/TestNG assertions introduce custom assertions • when private methods are not good enough • if you care about ubiquitous language • when “then” part is getting huge • to impress/confuse your team mates
  30. 30. The Evolution of Assertions
  31. 31. The Evolution of Assertions
  32. 32. Links AssertJ: http://joel-costigliola.github.io/assertj/ tools used to prepare these slides • http://shaky.github.bushong.net/ • http://hilite.me/ • http://asciiflow.com
  33. 33. Thank you! You can learn more about writing high quality tests by reading my books: „Practical Unit Testing” and „Bad Tests, Good Tests”. Please visit http://practicalunittesting.com to learn more.

×