Custom Assertions...when assertEquals() is not enough
Tomek Kaczanowski
freegreatpicture.com
A 15 minutes„lightning talk”
● Developer● Blogger● Books author
twitter: @tkaczanowskihttp://practicalunittesting.com
Tomek Kaczanowski
Expectations
our tests should:● verify
americanmethod.com
Expectations
our tests should:● verify● be readable
code.tutsplus.com
Expectations
our tests should:● verify● tell a story
jasonfurnell.wordpress.com
Expectations
our tests should:● verify● tell a story● be focused
noble.org
Expectations
our assertions should:● verify● tell a story● be focused
The Evolution of Assertions
The Evolution of Assertions
The Evolution of Assertions
The Evolution of Assertions
The Evolution of Assertions
Talk is cheap.Show me the code.
assertThat(...)
assertThat(...)
assertThat(...)
The Anatomy of Custom Assertion
The Anatomy of Custom Assertion
The Anatomy of Custom Assertion
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);...
Real Life Examples
Real Life Examples
actual
Real Life Examples
actual
assertThat(found) .overwritingErrorMessage( „expected row for {} but found only rows for {}”, stringDate, dates(actual) ) .isTrue();
Real Life Examples
actual
Conclusions
Upsides
● readable● reusable● simple to write● cheap to maintain
Upsides and Downsides
● need to write them● can hide bad smells
● readable● reusable● simple to write● cheap to maintain
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
The Evolution of Assertions
The Evolution of Assertions
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
Thank you!
You can learn more about writing high quality tests by
reading my books: „Practical Unit Testing”
and„Bad Tests, Good Tests”.
Please visithttp://practicalunittesting.com
to learn more.
Top Related