Unit testing basics and more...

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

Unit testing basics and more...

Talk about starting with JUnit and other popular tools given at Thessaloniki's Java Meetup

Papapetrou Patroklos

January 04, 2015

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

24 slides
  1. Unit testing basics & more... by Papapetrou P.Patroklos Twitter hashtag

    : #skg_unittesting @ppapapetrou76 / @skj_jug Thessaloniki Java Meetup - December 2014
  2. Agenda • Unit testing introduction • Differences with other types

    of tests • Key concepts • Rules - things to remember • A developer’s toolset • Live coding - hmm… unit testing actually @ppapapetrou76 / @skj_jug Thessaloniki Java Meetup - Unit Testing #skg_unittesting
  3. Unit testing? Why bother? “Code without tests is bad code.

    It doesn’t matter how well written it is; it doesn’t matter how pretty or object-oriented or well-encapsulated it is. With tests, we can change the behavior of our code quickly and verifiably. Without them, we really don’t know if our code is getting better or worse.” — Michael Feathers Working Effectively With Legacy Code (2004) @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java Meetup - Unit Testing
  4. Unit testing? Why bother? “Code without unit tests is like

    American breakfast without eggs & bacon. ” — Patroklos Papapetrou (2014) @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java Meetup - Unit Testing
  5. What is unit testing? A practice of testing certain functions

    and areas – or units – of our code. @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java Meetup - Unit Testing
  6. Advantages • Identify failures in our algorithms and/or logic •

    Approaching development from a unit testing perspective is that you'll likely be writing code that is easy to test • Prevent future changes from breaking functionality ( safety pillow ) @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java Meetup - Unit Testing
  7. (Good) Unit tests characteristics @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java

    Meetup - Unit Testing • Run fast • Isolated • Repeatable • Automatable • Easy to write • Easy to understand
  8. Unit test structure @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java Meetup

    - Unit Testing Setup 1 2 Prepare input Invoke method 3 4 Assert output Tear down 5
  9. Assertions? What is that? @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java

    Meetup - Unit Testing • Assertion -> A way to validate expected output / behavior of a method • Examples ◦ Assert.assertEquals (a, b) ◦ Assert.assertTrue ( bool ) • Each unit test should include only one assertion
  10. Mocks / Stubs / Fakes @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki

    Java Meetup - Unit Testing Simulated objects that mimic the behavior of real objects in controlled ways.
  11. Best Practices - Readability @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java

    Meetup - Unit Testing • Setup and teardown methods are not abused. It’s better to use factory methods for readability • Each test tests one thing only • Check for good and consistent naming conventions • Make sure that only meaningful assert messages are used, or none at all (meaningful test names are better)
  12. Best Practices - Readability @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java

    Meetup - Unit Testing • Asserts are separated from actions (different lines). • Tests don’t use magic strings and values as inputs. use the simplest inputs possible to prove your point. • There is consistency in location of tests. make it easy to find related tests for a method, or a class, or a project.
  13. Best Practices - Maintainability @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java

    Meetup - Unit Testing • Unit tests are isolated from each other and repeatable • Testing private or protected methods is not the norm (public is always better) • Tests are not over-specified • State-based testing is preferred over using interaction testing
  14. Best Practices - Maintainability @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java

    Meetup - Unit Testing • Strict mocks are used as little as possible (leads to over specification and fragile tests) • There is no more than one mock per test • Tests do not mix mocks and regular asserts in the same test (testing multiple things)
  15. Best Practices - Maintainability @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java

    Meetup - Unit Testing • Unit Tests ‘verify’ mock calls only on the single mock object in the test, and not on all the fake objects in the tests • Unit Tests verify only on a single call to a mock object. Verifying multiple calls on a mock object is either over specification or testing multiple things.
  16. Best Practices - Trust @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java

    Meetup - Unit Testing • Tests does not contain logic or dynamic values • Check coverage by playing with values (booleans or constants) • Unit tests are separated from integration tests • Unit tests don’t use things that keep changing in a unit test (like DateTime.Now ). Use fixed values.
  17. Best Practices - Trust @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java

    Meetup - Unit Testing • Unit tests don’t assert with expected values that are created dynamically - you might be repeating production code. • Unit tests should not do exception handling.
  18. Java Developer’s toolset @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java Meetup

    - Unit Testing • Basic Framework -> JUnit VS TestNG • Mocking Framework -> Mockito VS PowerMock VS EasyMock • Assertions Framework -> Hamcrest Matchers VS Fest Assert VS AssertJ • My Choice -> JUnit + Mockito + PowerMockito + AssertJ
  19. References @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java Meetup - Unit

    Testing • JUnit : http://junit.org • TestNG : http://testng.org • Mockito : https://code.google.com/p/mockito/ • PowerMock : https://code.google.com/p/powermock/ • PowerMockito : https://code.google. com/p/powermock/wiki/MockitoUsage13 • EasyMock : http://easymock.org/
  20. References @ppapapetrou76 / @skj_jug #skg_unittesting Thessaloniki Java Meetup - Unit

    Testing • Hamcrest Matchers : https://code.google.com/p/hamcrest/ • Fest Assertions : https://github.com/alexruiz/fest-assert-2.x • AssertJ: http://joel-costigliola.github.io/assertj/

More Decks by Papapetrou Patroklos

Other Decks in Programming