Practical Unit Testing with JUnit and Mockito - Unit Testing with JUnit and Mockito Practical Unit Testing with JUnit and Mockito ... 7 1.4. But Should Developers Test Their Own Code?!

  • View
    226

  • Download
    2

Embed Size (px)

Transcript

  • Practical Unit Testingwith JUnit and Mockito

    Tomek Kaczanowski

  • Practical Unit Testing with JUnit and Mockito

    Practical Unit Testing with JUnit and MockitoTomek Kaczanowski

    Copyright @ 2013 kaczanowscy.pl Tomasz Kaczanowski

    All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior writtenpermission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

    Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this bookis sold without warranty, either express or implied. Neither the author, nor the publisher, and its dealers and distributors will be held liable for any damagescaused or alleged to be caused directly or indirectly by this book.

    The author has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals.However, the author cannot guarantee the accuracy of this information.

    Visit us on the web: http://practicalunittesting.com

    Published by kaczanowscy.pl Tomasz Kaczanowski

    Cover design by Agata Wajer-Gadecka, http://poleznaku.pl

    ISBN: 978-83-934893-7-4

    First printing, April 2013

    Version pdf_a4_20130510_2149

    http://practicalunittesting.comhttp://poleznaku.pl

  • i

    DedicationTo my wife, Agnieszka, for all her support, and for having faith that I would eventually finish the book.

  • ii

    Table of ContentsAbout the Author ................................................................................................................................ viiAcknowledgments .............................................................................................................................. viiiPreface ................................................................................................................................................... xPreface - JUnit ................................................................................................................................... xiiiI. Developers' Tests ............................................................................................................................... 1

    1. On Tests and Tools .................................................................................................................. 21.1. An Object-Oriented System ........................................................................................... 21.2. Types of Developers' Tests ............................................................................................ 31.3. Verification and Design ................................................................................................. 71.4. But Should Developers Test Their Own Code?! ........................................................... 81.5. Tools Introduction .......................................................................................................... 9

    2. Unit Tests ................................................................................................................................ 132.1. What is a Unit Test? .................................................................................................... 132.2. Interactions in Unit Tests ............................................................................................ 14

    II. Writing Unit Tests ......................................................................................................................... 183. Unit Tests with no Collaborators ........................................................................................... 19

    3.1. Project Structure and Naming Conventions ................................................................ 193.2. Class To Test ............................................................................................................... 203.3. Your First JUnit Test ................................................................................................... 213.4. JUnit Assertions ........................................................................................................... 223.5. Failing Test .................................................................................................................. 233.6. Parameterized Tests ..................................................................................................... 243.7. Checking Expected Exceptions ................................................................................... 273.8. Test Fixture Setting ..................................................................................................... 293.9. Phases of a Unit Test .................................................................................................. 333.10. Conclusions ................................................................................................................ 343.11. Exercises .................................................................................................................... 36

    4. Test Driven Development ....................................................................................................... 394.1. When to Write Tests? .................................................................................................. 394.2. TDD Rhythm ............................................................................................................... 414.3. Benefits ........................................................................................................................ 474.4. TDD is Not Only about Unit Tests ............................................................................. 474.5. Test First Example ....................................................................................................... 484.6. Conclusions and Comments ........................................................................................ 564.7. How to Start Coding TDD .......................................................................................... 574.8. When not To Use Test-First? ...................................................................................... 584.9. Should I Follow It Blindly? ......................................................................................... 594.10. Exercises .................................................................................................................... 62

    5. Mocks, Stubs, Test Spies ........................................................................................................ 645.1. Introducing Mockito .................................................................................................... 645.2. Types of Test Double .................................................................................................. 715.3. Putting it All Together ................................................................................................. 765.4. Example: TDD with Test Doubles .............................................................................. 785.5. Always Use Test Doubles or Maybe Not? .............................................................. 895.6. Conclusions (with a Warning) ..................................................................................... 945.7. Exercises ...................................................................................................................... 96

    III. Hints and Discussions ................................................................................................................... 98

  • Practical Unit Testing with JUnit and Mockito

    iii

    6. Things You Should Know ...................................................................................................... 996.1. What Values To Check? .............................................................................................. 996.2. How to Fail a Test? ................................................................................................... 1026.3. How to Ignore a Test? ............................................................................................... 1036.4. More about Expected Exceptions .............................................................................. 1046.5. Stubbing Void Methods ............................................................................................. 1106.6. Matchers ..................................................................................................................... 1106.7. Mockito Matchers ...................................................................................................... 1146.8. Rules ........................................................................................................................... 1166.9. Unit Testing Asynchronous Code .............................................................................. 1206.10. Testing Thread Safe ................................................................................................. 1266.11. Time is not on Your Side ........................................................................................ 1286.12. Testing Collections .................................................................................................. 1336.13. Reading Test Data From Files ................................................................................. 1386.14. Conclusions .............................................................................................................. 1416.15. Exercises ..................................................................................