$30 off During Our Annual Pro Sale. View Details »

Unit testing basics and more...

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
Tweet

More Decks by Papapetrou Patroklos

Other Decks in Programming

Transcript

  1. Unit testing basics &
    more...
    by Papapetrou P.Patroklos
    Twitter hashtag : #skg_unittesting
    @ppapapetrou76 / @skj_jug
    Thessaloniki Java Meetup - December 2014

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  5. @ppapapetrou76 / @skj_jug #skg_unittesting
    Thessaloniki Java Meetup - Unit Testing

    View Slide

  6. 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

    View Slide

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

    View Slide

  8. Unit testing vs ??
    @ppapapetrou76 / @skj_jug #skg_unittesting
    Thessaloniki Java Meetup - Unit Testing
    We are here

    View Slide

  9. (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

    View Slide

  10. 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

    View Slide

  11. 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

    View Slide

  12. @ppapapetrou76 / @skj_jug #skg_unittesting
    Thessaloniki Java Meetup - Unit Testing

    View Slide

  13. 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.

    View Slide

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

    View Slide

  15. 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.

    View Slide

  16. 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

    View Slide

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

    View Slide

  18. 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.

    View Slide

  19. 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.

    View Slide

  20. 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.

    View Slide

  21. 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

    View Slide

  22. Let’s write some tests
    @ppapapetrou76 / @skj_jug #skg_unittesting
    Thessaloniki Java Meetup - Unit Testing

    View Slide

  23. 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/

    View Slide

  24. 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/

    View Slide