Introduction to Test Doubles

1,043 views

Published on

Slides used on my Online Training Class on Test Doubles

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,043
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
15
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Introduction to Test Doubles

  1. 1. Test Doubles The idea of test double comes from the “stunt doubles” that take the place of the actors in action movies. Source: xUnit Patterns, G. Meszaros http://xunitpatterns.com/Test%20Double.html
  2. 2. Types of doubles These are used in Unit Tests: ● ● ● ● Test Stub Test Spy Mock Object Test Dummy
  3. 3. The SUT and the double SUT: System Under Test (sometimes also called Subject Under Test) DOC: Depended-on Component ● ● We don't tests the doubles, we test/design the SUT ! Doubles are just the context, the dependencies
  4. 4. Stub “To get good enough control over the indirect inputs of the SUT, we may have to replace some of the context with something we can control, a Test Stub” - http://xunitpatterns.com/Test%20Stub.html ● ● Stubs can't remember calls Just return programmed answers
  5. 5. Spy “To get good enough visibility of the indirect outputs of the SUT, we may have to replace some of the context with something we can use to capture these outputs of the SUT”. http://xunitpatterns.com/Test%20Spy.html ● ● Spies remember calls, observ silently Don't have any expectations
  6. 6. Mock “In other cases, we must peer inside the SUT to be able to determine whether the expected behavior has occurred”. http://xunitpatterns.com/Mock%20Object.html ● ● ● Mocks expect certain calls to happen They fail if anything else happen They fail if expectations are not met
  7. 7. The problem with word “Mock” In English, all these doubles are mocks (as in mock chicken meat, the one made from gluten or soya). This is why some frameworks use the word “mock” even when they really use spies. That is the case of “mockito”. ● Understand the concepts beyond the names used by frameworks.
  8. 8. Isolation frameworks There are tools that allow us to stub out parts of the system even when the design doesn't allow for dependency injection. Let us test things that are apparently untestable. Example: Replacing the system funtion that gets the current Date and Time.

×