SlideShare a Scribd company logo
1 of 17
Download to read offline
Introduction      Test-Driven Development       EasyMock          Example   Summary




               Test-driven Development using JUnit and
                              EasyMock
                                  Developer’s Day 2010


                                    Patrick Schlebusch

                    Chair of Computer Science 5 - Information Systems


                                      October 5, 2010




                                                                               1 / 17
Introduction             Test-Driven Development   EasyMock   Example   Summary



Overview


       1       Introduction

       2       Test-Driven Development

       3       EasyMock

       4       Example

       5       Summary




                                                                           2 / 17
Introduction          Test-Driven Development   EasyMock        Example   Summary



Why Testing?




               testing is usually not a popular task
               → affects quality of tests
               testing is necessary
               → you will have to it anyway
               but: testing is not so bad, if you use the right tools




                                                                             3 / 17
Introduction           Test-Driven Development   EasyMock        Example       Summary



Testing Basics


               What is testing?
               → checking actual behavior against specified/expected behavior
               Why do we test?
                   finding bugs
                   establishing confidence & trust in the software
               kinds of testing:
                    black-box: without knowledge of internal structure
                    white-box: with knowledge of internal structure
               types of tests:
                    unit test
                    integration test
                    system test



                                                                                  4 / 17
Introduction          Test-Driven Development   EasyMock       Example          Summary



Motivation



               developers spend too much time debugging
               writing tests afterwards causes problems:
                   it is tedious (”it works, so why bother?”)
                   no independence
                   → low quality of tests (the code does, what the code does)
               independence is important for tests, but often not
               incorporated
               losing focus on the requirements during development




                                                                                   5 / 17
Introduction          Test-Driven Development             EasyMock   Example   Summary



Concepts




                                            Drawing: Tim Ottinger




               evolutionary test and implementation
               always write the test before the code and let it fail first
               fast, automated tests that can be run frequently



                                                                                  6 / 17
Introduction          Test-Driven Development   EasyMock        Example      Summary



Pros & Cons


               noticing defects right when they are introduced
               → much less time spent debugging
               if followed strictly, a very high test coverage is achieved
               → higher quality, less undiscovered bugs
               design and code will be more testable
               forces the developer to think about the requirements before
               each change

               hard to learn
               tests need to be maintained too
               thorough unit tests cannot replace integration & system tests


                                                                                7 / 17
Introduction          Test-Driven Development    EasyMock         Example      Summary



What is Mocking?



               we want to test units in isolation
                   to be able to locate errors easily
                   but: most units communicate with other units
               create a test environment to simulate behavior of other units
               verify when, where and which data is passed by a unit

               mock-objects
                   contain no functionality, but can return test values
                   allow verifying called methods, parameters, etc.




                                                                                  8 / 17
Introduction          Test-Driven Development   EasyMock     Example          Summary



EasyMock



               Open Source Mocking framework for Java
               available from http://easymock.org
               first presented at OOPSLA 2001
               dynamically creates mock objects from classes and interfaces
               provides methods for specifying and verifying expectations and
               stub behavior
               much more convenient than hand-written mocks/stubs




                                                                                 9 / 17
Introduction          Test-Driven Development    EasyMock            Example   Summary



Mock Types

               Mock
                   only explicitly specified calls are expected
                   order of calls is not verified
               NiceMock
                   calls that are not expected don’t fail the test
                   unexpected calls return default null values
                   order of calls is not verified
               StrictMock
                   only explicitly specified calls are expected
                   order of calls is verified


               Partial mock
                   only some methods are mocked
                   object can still contain functionality in some methods

                                                                                 10 / 17
Introduction          Test-Driven Development   EasyMock         Example        Summary



Defining Expectations



               after creation the Mock is in record-mode
               all calls made to this object will be recorded as expectations
               methods expect and expectLastCall can be used
                   to specify a return value or exception to be thrown
                   to specify how often this call should be expected


               but: should be used carefully, otherwise implementation is
               restricted too much




                                                                                  11 / 17
Introduction           Test-Driven Development   EasyMock      Example       Summary



Replaying and Verifying Mocks




               after recording expected calls, mocks can be set to replay state
               in replay state mocks behaves as defined for the test
               after the test, mocks can be verified
               checks whether all expected calls happened
               if not, the test fails




                                                                                  12 / 17
Introduction          Test-Driven Development   EasyMock     Example        Summary



Matchers and Captures


               when specifying behavior you might not know parameter
               values of method calls
               → Matchers
               matchers allow matching parameter values
                   for example by class or value
                   can be combined (and, or, not)
                   own matchers can be implemented
               you might want to intercept passed parameters to inspect
               them
               → Captures
               captures allow capturing parameters that are passed to the
               mock


                                                                              13 / 17
Introduction          Test-Driven Development   EasyMock        Example   Summary



Limitations of EasyMock




               some technical limitations:
                   mocking of final methods is not possible
                   mocking of private methods is not possible
                   mocking of static methods is not possible


               unfortunately fail/error messages are often misleading




                                                                            14 / 17
Introduction   Test-Driven Development   EasyMock   Example   Summary



Example Demonstration




                                                                15 / 17
Introduction          Test-Driven Development   EasyMock      Example   Summary



Summary



               TDD helps writing better tests
               takes the tediousness out of testing
               still requires commitment

               mocking is a practical tool for Unit-testing
               especially useful for TDD
               mocking has (technical) limitations
               should be used responsibly!




                                                                          16 / 17
Introduction   Test-Driven Development   EasyMock   Example   Summary



Thanks..




                  Thank you for your attention!




                                                                17 / 17

More Related Content

What's hot

Junit, mockito, etc
Junit, mockito, etcJunit, mockito, etc
Junit, mockito, etcYaron Karni
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with MockitoRichard Paul
 
All about unit testing using (power) mock
All about unit testing using (power) mockAll about unit testing using (power) mock
All about unit testing using (power) mockPranalee Rokde
 
Mockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworksMockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworksEndranNL
 
Embrace Unit Testing
Embrace Unit TestingEmbrace Unit Testing
Embrace Unit Testingalessiopace
 
Xp Day 080506 Unit Tests And Mocks
Xp Day 080506 Unit Tests And MocksXp Day 080506 Unit Tests And Mocks
Xp Day 080506 Unit Tests And Mocksguillaumecarre
 
Programmer testing
Programmer testingProgrammer testing
Programmer testingJoao Pereira
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesNarendra Pathai
 

What's hot (20)

Power mock
Power mockPower mock
Power mock
 
Mock with Mockito
Mock with MockitoMock with Mockito
Mock with Mockito
 
Junit, mockito, etc
Junit, mockito, etcJunit, mockito, etc
Junit, mockito, etc
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
Unit testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
JMockit
JMockitJMockit
JMockit
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with Mockito
 
All about unit testing using (power) mock
All about unit testing using (power) mockAll about unit testing using (power) mock
All about unit testing using (power) mock
 
Mockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworksMockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworks
 
Embrace Unit Testing
Embrace Unit TestingEmbrace Unit Testing
Embrace Unit Testing
 
Using Mockito
Using MockitoUsing Mockito
Using Mockito
 
Xp Day 080506 Unit Tests And Mocks
Xp Day 080506 Unit Tests And MocksXp Day 080506 Unit Tests And Mocks
Xp Day 080506 Unit Tests And Mocks
 
Programmer testing
Programmer testingProgrammer testing
Programmer testing
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Best practices unit testing
Best practices unit testing Best practices unit testing
Best practices unit testing
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practices
 
Unit Testing 101
Unit Testing 101Unit Testing 101
Unit Testing 101
 
Junit
JunitJunit
Junit
 
Mock your way with Mockito
Mock your way with MockitoMock your way with Mockito
Mock your way with Mockito
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 

Similar to Testdriven Development using JUnit and EasyMock

Unit testing for 40 square software
Unit testing for 40 square softwareUnit testing for 40 square software
Unit testing for 40 square softwareRuben Tan
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development CodeOps Technologies LLP
 
Development without Testers: Myth or Real Option? (ConfeT&QA conference)
Development without Testers: Myth or Real Option? (ConfeT&QA conference)Development without Testers: Myth or Real Option? (ConfeT&QA conference)
Development without Testers: Myth or Real Option? (ConfeT&QA conference)Mikalai Alimenkou
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing FundamentalsRichard Paul
 
Development without Testers: Myth or Real Option?
Development without Testers: Myth or Real Option?Development without Testers: Myth or Real Option?
Development without Testers: Myth or Real Option?Mikalai Alimenkou
 
Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUGlburdz
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Hong Le Van
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in AngularKnoldus Inc.
 
Swiss Testing Day - Testautomation, 10 (sometimes painful) lessons learned
Swiss Testing Day - Testautomation, 10 (sometimes painful) lessons learnedSwiss Testing Day - Testautomation, 10 (sometimes painful) lessons learned
Swiss Testing Day - Testautomation, 10 (sometimes painful) lessons learnedMichael Palotas
 
Unit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking SkeletonUnit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking SkeletonSeb Rose
 
Software Design for Testability
Software Design for TestabilitySoftware Design for Testability
Software Design for Testabilityamr0mt
 
Automated Testing Of EPiServer CMS Sites
Automated Testing Of EPiServer CMS SitesAutomated Testing Of EPiServer CMS Sites
Automated Testing Of EPiServer CMS Sitesjoelabrahamsson
 

Similar to Testdriven Development using JUnit and EasyMock (20)

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Testing w-mocks
Testing w-mocksTesting w-mocks
Testing w-mocks
 
Unit testing for 40 square software
Unit testing for 40 square softwareUnit testing for 40 square software
Unit testing for 40 square software
 
Unit testing
Unit testingUnit testing
Unit testing
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
Development without Testers: Myth or Real Option? (ConfeT&QA conference)
Development without Testers: Myth or Real Option? (ConfeT&QA conference)Development without Testers: Myth or Real Option? (ConfeT&QA conference)
Development without Testers: Myth or Real Option? (ConfeT&QA conference)
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
 
Development without Testers: Myth or Real Option?
Development without Testers: Myth or Real Option?Development without Testers: Myth or Real Option?
Development without Testers: Myth or Real Option?
 
Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUG
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
 
Agile Testing
Agile TestingAgile Testing
Agile Testing
 
Simple testable code
Simple testable codeSimple testable code
Simple testable code
 
Swiss Testing Day - Testautomation, 10 (sometimes painful) lessons learned
Swiss Testing Day - Testautomation, 10 (sometimes painful) lessons learnedSwiss Testing Day - Testautomation, 10 (sometimes painful) lessons learned
Swiss Testing Day - Testautomation, 10 (sometimes painful) lessons learned
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Test driven development(tdd)
Test driven development(tdd)Test driven development(tdd)
Test driven development(tdd)
 
Unit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking SkeletonUnit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking Skeleton
 
Software Design for Testability
Software Design for TestabilitySoftware Design for Testability
Software Design for Testability
 
Automated Testing Of EPiServer CMS Sites
Automated Testing Of EPiServer CMS SitesAutomated Testing Of EPiServer CMS Sites
Automated Testing Of EPiServer CMS Sites
 

Testdriven Development using JUnit and EasyMock

  • 1. Introduction Test-Driven Development EasyMock Example Summary Test-driven Development using JUnit and EasyMock Developer’s Day 2010 Patrick Schlebusch Chair of Computer Science 5 - Information Systems October 5, 2010 1 / 17
  • 2. Introduction Test-Driven Development EasyMock Example Summary Overview 1 Introduction 2 Test-Driven Development 3 EasyMock 4 Example 5 Summary 2 / 17
  • 3. Introduction Test-Driven Development EasyMock Example Summary Why Testing? testing is usually not a popular task → affects quality of tests testing is necessary → you will have to it anyway but: testing is not so bad, if you use the right tools 3 / 17
  • 4. Introduction Test-Driven Development EasyMock Example Summary Testing Basics What is testing? → checking actual behavior against specified/expected behavior Why do we test? finding bugs establishing confidence & trust in the software kinds of testing: black-box: without knowledge of internal structure white-box: with knowledge of internal structure types of tests: unit test integration test system test 4 / 17
  • 5. Introduction Test-Driven Development EasyMock Example Summary Motivation developers spend too much time debugging writing tests afterwards causes problems: it is tedious (”it works, so why bother?”) no independence → low quality of tests (the code does, what the code does) independence is important for tests, but often not incorporated losing focus on the requirements during development 5 / 17
  • 6. Introduction Test-Driven Development EasyMock Example Summary Concepts Drawing: Tim Ottinger evolutionary test and implementation always write the test before the code and let it fail first fast, automated tests that can be run frequently 6 / 17
  • 7. Introduction Test-Driven Development EasyMock Example Summary Pros & Cons noticing defects right when they are introduced → much less time spent debugging if followed strictly, a very high test coverage is achieved → higher quality, less undiscovered bugs design and code will be more testable forces the developer to think about the requirements before each change hard to learn tests need to be maintained too thorough unit tests cannot replace integration & system tests 7 / 17
  • 8. Introduction Test-Driven Development EasyMock Example Summary What is Mocking? we want to test units in isolation to be able to locate errors easily but: most units communicate with other units create a test environment to simulate behavior of other units verify when, where and which data is passed by a unit mock-objects contain no functionality, but can return test values allow verifying called methods, parameters, etc. 8 / 17
  • 9. Introduction Test-Driven Development EasyMock Example Summary EasyMock Open Source Mocking framework for Java available from http://easymock.org first presented at OOPSLA 2001 dynamically creates mock objects from classes and interfaces provides methods for specifying and verifying expectations and stub behavior much more convenient than hand-written mocks/stubs 9 / 17
  • 10. Introduction Test-Driven Development EasyMock Example Summary Mock Types Mock only explicitly specified calls are expected order of calls is not verified NiceMock calls that are not expected don’t fail the test unexpected calls return default null values order of calls is not verified StrictMock only explicitly specified calls are expected order of calls is verified Partial mock only some methods are mocked object can still contain functionality in some methods 10 / 17
  • 11. Introduction Test-Driven Development EasyMock Example Summary Defining Expectations after creation the Mock is in record-mode all calls made to this object will be recorded as expectations methods expect and expectLastCall can be used to specify a return value or exception to be thrown to specify how often this call should be expected but: should be used carefully, otherwise implementation is restricted too much 11 / 17
  • 12. Introduction Test-Driven Development EasyMock Example Summary Replaying and Verifying Mocks after recording expected calls, mocks can be set to replay state in replay state mocks behaves as defined for the test after the test, mocks can be verified checks whether all expected calls happened if not, the test fails 12 / 17
  • 13. Introduction Test-Driven Development EasyMock Example Summary Matchers and Captures when specifying behavior you might not know parameter values of method calls → Matchers matchers allow matching parameter values for example by class or value can be combined (and, or, not) own matchers can be implemented you might want to intercept passed parameters to inspect them → Captures captures allow capturing parameters that are passed to the mock 13 / 17
  • 14. Introduction Test-Driven Development EasyMock Example Summary Limitations of EasyMock some technical limitations: mocking of final methods is not possible mocking of private methods is not possible mocking of static methods is not possible unfortunately fail/error messages are often misleading 14 / 17
  • 15. Introduction Test-Driven Development EasyMock Example Summary Example Demonstration 15 / 17
  • 16. Introduction Test-Driven Development EasyMock Example Summary Summary TDD helps writing better tests takes the tediousness out of testing still requires commitment mocking is a practical tool for Unit-testing especially useful for TDD mocking has (technical) limitations should be used responsibly! 16 / 17
  • 17. Introduction Test-Driven Development EasyMock Example Summary Thanks.. Thank you for your attention! 17 / 17