0
Introduction      Test-Driven Development       EasyMock          Example   Summary




               Test-driven Develop...
Introduction             Test-Driven Development   EasyMock   Example   Summary



Overview


       1       Introduction
...
Introduction          Test-Driven Development   EasyMock        Example   Summary



Why Testing?




               testi...
Introduction           Test-Driven Development   EasyMock        Example       Summary



Testing Basics


               ...
Introduction          Test-Driven Development   EasyMock       Example          Summary



Motivation



               de...
Introduction          Test-Driven Development             EasyMock   Example   Summary



Concepts




                   ...
Introduction          Test-Driven Development   EasyMock        Example      Summary



Pros & Cons


               notic...
Introduction          Test-Driven Development    EasyMock         Example      Summary



What is Mocking?



            ...
Introduction          Test-Driven Development   EasyMock     Example          Summary



EasyMock



               Open S...
Introduction          Test-Driven Development    EasyMock            Example   Summary



Mock Types

               Mock
...
Introduction          Test-Driven Development   EasyMock         Example        Summary



Defining Expectations



       ...
Introduction           Test-Driven Development   EasyMock      Example       Summary



Replaying and Verifying Mocks




...
Introduction          Test-Driven Development   EasyMock     Example        Summary



Matchers and Captures


           ...
Introduction          Test-Driven Development   EasyMock        Example   Summary



Limitations of EasyMock




         ...
Introduction   Test-Driven Development   EasyMock   Example   Summary



Example Demonstration




                       ...
Introduction          Test-Driven Development   EasyMock      Example   Summary



Summary



               TDD helps wri...
Introduction   Test-Driven Development   EasyMock   Example   Summary



Thanks..




                  Thank you for your...
Upcoming SlideShare
Loading in...5
×

Testdriven Development using JUnit and EasyMock

4,426

Published on

My talk on TDD using JUnit and EasyMock for Developer's Day 2010 @ I5.

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,426
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
136
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Testdriven Development using JUnit and EasyMock"

  1. 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. 2. Introduction Test-Driven Development EasyMock Example Summary Overview 1 Introduction 2 Test-Driven Development 3 EasyMock 4 Example 5 Summary 2 / 17
  3. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 15. Introduction Test-Driven Development EasyMock Example Summary Example Demonstration 15 / 17
  16. 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. 17. Introduction Test-Driven Development EasyMock Example Summary Thanks.. Thank you for your attention! 17 / 17
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×