Mastering Unit Testing Using Mockito and JUnit
eBook - ePub

Mastering Unit Testing Using Mockito and JUnit

Sujoy Acharya

Partager le livre
  1. 314 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Mastering Unit Testing Using Mockito and JUnit

Sujoy Acharya

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

In Detail

It is insanity to keep doing things the same way and expect them to improve. Any program is useful only when it is functional; hence, before applying complex tools, patterns, or APIs to your production code, checking software functionality is must. Automated JUnit tests help you verify your assumptions continuously, detect side effects quickly, and also help you save time.

This book will provide the skills you need to successfully build and maintain meaningful JUnit test cases. You will begin with how to use advanced JUnit 4 features, improve code coverage, automate JUnit tests, monitor code quality, write JUnit tests for the database and web tier refactor legacy code, mock external dependencies using Mockito, and write testable code using test-driven development. By sequentially working through the steps in each chapter, you will quickly master the advanced JUnit features.

Approach

A practical and easy-to-follow, yet comprehensive, guide to learning advanced JUnit testing. Each topic is explained and placed in context, and for the more inquisitive, there are more details of the concepts used.

Who this book is for

This book is for you if you are a developer with some experience in Java application development as well as a basic knowledge of JUnit testing. But for those whose skill set is void of any prior experience with JUnit testing, the book also covers basic fundamentals to get you acquainted with the concepts before putting them into practise.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Mastering Unit Testing Using Mockito and JUnit est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Mastering Unit Testing Using Mockito and JUnit par Sujoy Acharya en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Informatique et DĂ©veloppement de logiciels. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2014
ISBN
9781783982509

Mastering Unit Testing Using Mockito and JUnit


Table of Contents

Mastering Unit Testing Using Mockito and JUnit
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. JUnit 4 – a Total Recall
Defining unit testing
Working with JUnit 4
Setting up Eclipse
Running the first unit test
Exploring annotations
Verifying test conditions with Assertion
Working with exception handling
Exploring the @RunWith annotation
Working with JUnit 4++
Ignoring a test
Executing tests in order
Learning assumptions
Exploring the test suite
Asserting with assertThat
Comparing matchers – equalTo, is, and not
Working with compound value matchers – either, both, anyOf, allOf, and not
Working with collection matchers – hasItem and hasItems
Exploring string matchers – startsWith, endsWith, and containsString
Exploring built-in matchers
Building a custom matcher
Creating parameterized tests
Working with parameterized constructors
Working with parameterized methods
Giving a name
Working with timeouts
Exploring JUnit theories
Externalizing data using @ParametersSuppliedBy and ParameterSupplier
Dealing with JUnit rules
Playing with the timeout rule
Working with the ExpectedException rule
Unfolding the TemporaryFolder rule
Exploring the ErrorCollector rule
Working with the Verifier rule
Learning the TestWatcher rule
Working with the TestName rule
Handling external resources
Exploring JUnit categories
Summary
2. Automating JUnit Tests
Continuous Integration
Benefits of CI
Gradle automation
Getting started
Gradling
Ordering subtasks using doFirst and doLast
Default tasks
The task dependency
Daemon
Gradle plugins
The Eclipse plugin
The Java plugin
Maven project management
Installation
The Archetype plugin
The Project Object Model (POM) file
Project dependency
The build life cycle
Compiling the project
Testing the project
Packaging the project
The clean life cycle
The site life cycle
Another neat tool (Ant)
Jenkins
The Gradle project
The Maven project
Building the Ant project
Summary
3. Test Doubles
Dummy
Stub
Fake
Mock
Spy
Summary
4. Progressive Mockito
Working with Mockito
Why should you use Mockito?
Qualities of unit testing
Drinking Mockito
Configuring Mockito
Mocking in action
Mocking objects
Stubbing methods
Verifying the method invocation
Why do we use verify?
Verifying in depth
Verifying zero and no more interactions
Using argument matcher
Why do we need wildcard matchers?
The ArgumentMatcher class
Throwing exceptions
Stubbing consecutive calls
Stubbing with an Answer
Spying objects
Stubbing void methods
Capturing arguments with ArgumentCaptor
Verifying the invocation order
Changing the default settings
Resetting mock objects
Exploring Mockito annotations
Working with inline stubbing
Determining mocking details
Behavior-driven development with Mockito
Writing tests in BDD style
The BDD syntax
Summary
5. Exploring Code Coverage
Understanding code coverage
Learning the inner details of code instrumentation
Configuring the Eclipse plugin
Uncovering the Clover plugin
Working with the EclEmma plugin
Examining the eCobertura plugin
Measuring coverage using Gradle
Working with the Maven Cobertura plugin
Running the Cobertura Ant task
Summary
6. Revealing Code Quality
Understanding the static code analysis
Working with the Checkstyle plugin
Exploring the FindBugs plugin
Working with the PMD plugin
Monitoring code quality with SonarQube
Running SonarQube
Analyzing code with the SonarQube runner
Improving quality with the Sonar Eclipse plugin
Monitoring quality with Gradle and SonarQube
Monitoring quality with Maven and SonarQube
Monitoring quality with Ant and SonarQube
Getting familiar with false positives
Summary
7. Unit Testing the Web Tier
Unit testing servlets
Building and unit testing a J2EE web application
Playing with Spring MVC
Summary
8. Playing with Data
Separating concerns
Unit testing the persistence logic
Simplifying persistence with Spring
Verifying the system integrity
Writing integration tests with Spring
Summary
9. Solving Test Puzzles
Working with the legacy code
Working with testing impediments
Designing for testability
Identifying constructor issues
Realizing initialization issues
Working with private methods
Working with final methods
Exploring static method issues
Working with final classes
Learning the new attribute
Exploring static variables and blocks
Working with greenfield code
Summary
10. Best Practices
Writing meaningful tests
Improving readability
Breaking everything that could possibly break
Ignoring simple test cases
Verifying invalid parameters
Relying on direct testing
Staying away from debugging
Avoiding generic matchers
Keeping away from @ignore
Eluding debug messages
Automating JUnit tests
Configuring tests
Running in-memory tests
Staying away from Thread.sleep
Keeping unit tests away from the production code
Avoiding static variables
Assuming the test execution order
Loading data from files
Invoking super.setUp() and super.tearDown()
Staying away from side effects
Working with locales
Working with dates
Working with assertions
Using the correct assertion
Maintaining the assertEquals parameter order
Striving for one assertion per test
Handling exceptions
Working with test smells
Refactoring duplicates
Refactoring the test control logic
Removing the test logic from the production code
Refactoring over engineered tests
Summary
Index

Mastering Unit Test...

Table des matiĂšres