Top Banner
Custom Assertions ...when assertEquals() is not enough Tomek Kaczanowski
36

2014 GeeCON Custom Assertions

May 11, 2015

Download

Software

my GeeCON 2014 "lightning talk" about custom assertions
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 2014 GeeCON Custom Assertions

Custom Assertions...when assertEquals() is not enough

Tomek Kaczanowski

Page 2: 2014 GeeCON Custom Assertions

freegreatpicture.com

A 15 minutes„lightning talk”

Page 3: 2014 GeeCON Custom Assertions

● Developer● Blogger● Books author

twitter: @tkaczanowskihttp://practicalunittesting.com

Tomek Kaczanowski

Page 4: 2014 GeeCON Custom Assertions
Page 5: 2014 GeeCON Custom Assertions
Page 6: 2014 GeeCON Custom Assertions
Page 7: 2014 GeeCON Custom Assertions

Expectations

our tests should:● verify

americanmethod.com

Page 8: 2014 GeeCON Custom Assertions

Expectations

our tests should:● verify● be readable

code.tutsplus.com

Page 9: 2014 GeeCON Custom Assertions

Expectations

our tests should:● verify● tell a story

jasonfurnell.wordpress.com

Page 10: 2014 GeeCON Custom Assertions

Expectations

our tests should:● verify● tell a story● be focused

noble.org

Page 11: 2014 GeeCON Custom Assertions

Expectations

our assertions should:● verify● tell a story● be focused

Page 12: 2014 GeeCON Custom Assertions

The Evolution of Assertions

Page 13: 2014 GeeCON Custom Assertions

The Evolution of Assertions

Page 14: 2014 GeeCON Custom Assertions

The Evolution of Assertions

Page 15: 2014 GeeCON Custom Assertions

The Evolution of Assertions

Page 16: 2014 GeeCON Custom Assertions

The Evolution of Assertions

Page 17: 2014 GeeCON Custom Assertions

Talk is cheap.Show me the code.

Page 18: 2014 GeeCON Custom Assertions

assertThat(...)

Page 19: 2014 GeeCON Custom Assertions

assertThat(...)

Page 20: 2014 GeeCON Custom Assertions

assertThat(...)

Page 21: 2014 GeeCON Custom Assertions

The Anatomy of Custom Assertion

Page 22: 2014 GeeCON Custom Assertions

The Anatomy of Custom Assertion

Page 23: 2014 GeeCON Custom Assertions

The Anatomy of Custom Assertion

Page 24: 2014 GeeCON Custom Assertions

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);...

Page 25: 2014 GeeCON Custom Assertions

Real Life Examples

Page 26: 2014 GeeCON Custom Assertions

Real Life Examples

actual

Page 27: 2014 GeeCON Custom Assertions

Real Life Examples

actual

assertThat(found) .overwritingErrorMessage( „expected row for {} but found only rows for {}”, stringDate, dates(actual) ) .isTrue();

Page 28: 2014 GeeCON Custom Assertions

Real Life Examples

actual

Page 29: 2014 GeeCON Custom Assertions

Conclusions

Page 30: 2014 GeeCON Custom Assertions

Upsides

● readable● reusable● simple to write● cheap to maintain

Page 31: 2014 GeeCON Custom Assertions

Upsides and Downsides

● need to write them● can hide bad smells

● readable● reusable● simple to write● cheap to maintain

Page 32: 2014 GeeCON Custom Assertions

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

Page 33: 2014 GeeCON Custom Assertions

The Evolution of Assertions

Page 34: 2014 GeeCON Custom Assertions

The Evolution of Assertions

Page 35: 2014 GeeCON Custom 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

Page 36: 2014 GeeCON Custom Assertions

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.