Mock Object




2011   3   5
How it works

               SUT

               Mock Object   SUT




2011   3   5
When to use it

               Untested Requirement
                   (pp.273)




                                 ↑

2011   3   5
Mock Object
                          Test Stub


                                      equals


                                                   Mock Object


                 equals                        →Equality Pollution
               (pp.217)




2011   3   5
Mock Object                  Equality
               Assertions           comparator




               Mock Object




                     Mock Object   Mock Object


                              Mock Object



2011   3   5
Implementation Notes

               Mock Object        SUT




                      Test Spy



2011   3   5
Mock Object                       4
                      fixture
                      3
                          result verification phase
                  (“final verification”
                  )




2011   3   5
Fixture Setup
               MockObject         /


               Exercise SUT
               MocK Object


               Result verification
                        “final verification”


               Fixture teardown




2011   3   5
Construction
               Four-Phase Test   fixture setup phase
                        Mock Object




               Configuration with Expected Values
               Test Double                     Mock
               Object


               →Hard-Coded Test Double


2011   3   5
Installation
               Test Double SUT




               SUT


               Dependency Injection   Dependency Lookup




2011   3   5
Usage
               SUT Mock Object




               Assertion
               (      Mock Object


                                    )


2011   3   5
Mock Object   SUT


                             →happy path




2011   3   5
Final Verification
                          1


                         Mock Object toolkits    teardown


                                       final Verification
               Method




2011   3   5
Motivating Example




2011   3   5
Refactoring Notes
                       Test Double

               Mock Object
                                 SUT



                             Mock Object                 1
                                     final verification method




2011   3   5
2011   3   5
2011   3   5
JUnit3      Mock Object    TestCase
                                    Assertion Method   static




                   xUnit Family      NUnit   static
                         Assert


                  Ruby Test::Unit        mixins
               Assert



2011   3   5
Example: Mock Object
               (Dynamically Generated)




2011   3   5
Further Reading

                  xUnit                Mock Object


                                        Mock Object
               Test Stub Fake Object


                →Appnendix B(pp.741)



2011   3   5

Mock object

  • 1.
  • 2.
    How it works SUT Mock Object SUT 2011 3 5
  • 3.
    When to useit Untested Requirement (pp.273) ↑ 2011 3 5
  • 4.
    Mock Object Test Stub equals Mock Object equals →Equality Pollution (pp.217) 2011 3 5
  • 5.
    Mock Object Equality Assertions comparator Mock Object Mock Object Mock Object Mock Object 2011 3 5
  • 6.
    Implementation Notes Mock Object SUT Test Spy 2011 3 5
  • 7.
    Mock Object 4 fixture 3 result verification phase (“final verification” ) 2011 3 5
  • 8.
    Fixture Setup MockObject / Exercise SUT MocK Object Result verification “final verification” Fixture teardown 2011 3 5
  • 9.
    Construction Four-Phase Test fixture setup phase Mock Object Configuration with Expected Values Test Double Mock Object →Hard-Coded Test Double 2011 3 5
  • 10.
    Installation Test Double SUT SUT Dependency Injection Dependency Lookup 2011 3 5
  • 11.
    Usage SUT Mock Object Assertion ( Mock Object ) 2011 3 5
  • 12.
    Mock Object SUT →happy path 2011 3 5
  • 13.
    Final Verification 1 Mock Object toolkits teardown final Verification Method 2011 3 5
  • 14.
  • 15.
    Refactoring Notes Test Double Mock Object SUT Mock Object 1 final verification method 2011 3 5
  • 16.
    2011 3 5
  • 17.
    2011 3 5
  • 18.
    JUnit3 Mock Object TestCase Assertion Method static xUnit Family NUnit static Assert Ruby Test::Unit mixins Assert 2011 3 5
  • 19.
    Example: Mock Object (Dynamically Generated) 2011 3 5
  • 20.
    Further Reading xUnit Mock Object Mock Object Test Stub Fake Object →Appnendix B(pp.741) 2011 3 5