software engineering
play

Software Engineering and Architecture Test Stubs and Doubles - PowerPoint PPT Presentation

Software Engineering and Architecture Test Stubs and Doubles getting the world under test control GammaTowns RateStrategy But how to test? How do I TDD it? Read system clock to etc. etc. determine if weekend CS, AU Henrik Brbak


  1. Software Engineering and Architecture Test Stubs and Doubles … getting the world under test control

  2. GammaTown’s RateStrategy But how to test? How do I TDD it? Read system clock to etc. etc. determine if weekend CS, AU Henrik Bærbak Christensen 2

  3. Tricky Requirement • The test case for AlphaTown: • … problematic for GammaTown … CS, AU Henrik Bærbak Christensen 3

  4. Analysis • Gammatown, however, has one more parameter in the rate policy test case • The problem is This parameter is not accessible from the testing code! CS, AU Henrik Bærbak Christensen 4

  5. Code view ??? Direct input parameter: payment Indirect input parameter: day of week CS, AU Henrik Bærbak Christensen 5

  6. TDD of State Pattern • To implement GammaTown requirements I do it manually CS, AU Henrik Bærbak Christensen 6

  7. But it is bad … • After introducing Gammatown I no longer have automated tests because I have to run some of the tests during the weekend. – I have a ‘manual run on weekend and another run on weekdays targets’ • I want to get back to as much automated testing as possible. CS, AU Henrik Bærbak Christensen 7

  8. Analysis of Parameters ??? Direct input parameter: payment Indirect input parameter: day of week CS, AU Henrik Bærbak Christensen 8

  9. Definitions • This reflection allows me to classify parameters: • UUT = Unit Under Test. – here it is the AlternatingRateStrategy instance... CS, AU Henrik Bærbak Christensen 9

  10. Where does indirect input come from? • So the 1000$ question is: where does the indirect input parameter come from? • Exercise: Name other types of indirect input? CS, AU Henrik Bærbak Christensen 10

  11. Analysis: Code view • Structure of xUnit test cases – Collaboration diagram: interaction between objects • DOU = Depended On Unit CS, AU Henrik Bærbak Christensen 11

  12. Direct versus Indirect Direct input Indirect input CS, AU Henrik Bærbak Christensen 12

  13. The Gammatown Rate Policy • My DOU is the Java system clock: System Clock Test code AlternatingRateStrategy System.DateTime java.util.Calendar Method parameters Calling library methods CS, AU Henrik Bærbak Christensen 13

  14. The Challenge • This analysis allows me to state the challenge: System Clock System.DateTime Test code AlternatingRateStrategy java.util.Calendar • How can I make the DOU return values that are defined by the testing code? CS, AU Henrik Bærbak Christensen 14

  15. Analysis • Basically it is a variability problem – During testing, use data given by test code – During normal ops, use data given by system • So I can reuse my previous analysis – parametric proposal – polymorphic proposal Scientists like to do this all the time! If – compositional proposal we can rephrase a new question into an old one, whose answer is known – then we are done ☺ CS, AU Henrik Bærbak Christensen 15

  16. Parametric • This is perhaps the oldest solution in the C world • #ifdef DEBUG • today = PRESET_VALUE; • #else • today = (get date from clock); • # • return today == Saturday || today == Sunday; CS, AU Henrik Bærbak Christensen 16

  17. Polymorphic • Subclass or die... AlternatingRateStrategy Override ‘isWeekend()’ method TestAlternatingRateStrategy • Actually a quite reasonable approach... • Argue why!!! CS, AU Henrik Bærbak Christensen 17

  18. Compositional • 3-1-2 leads to yet another Strategy Pattern: CS, AU Henrik Bærbak Christensen 18

  19. Static Architecture View • Exercise: Why is this Strategy and not State? CS, AU Henrik Bærbak Christensen 19

  20. Production Code CS, AU Henrik Bærbak Christensen 20

  21. The Stub CS, AU Henrik Bærbak Christensen 21

  22. Test Code CS@AU Henrik Bærbak Christensen 22

  23. Rephrasing as Test Case Direct input parameter: payment Now: Direct input parameter: weekend or not CS, AU Henrik Bærbak Christensen 23

  24. Sorry Bob  • I had not read Uncle Bob when I wrote the book – What property is the present code missing? CS@AU Henrik Bærbak Christensen 24

  25. Today I would… • … introduce an Enum type – No flag argument, replaced by descriptive names IS_WEEKDAY IS_WEEKEND CS@AU Henrik Bærbak Christensen 25

  26. Test Stub • I have made a test stub CS, AU Henrik Bærbak Christensen 26

  27. Key point • The use of Test doubles is a key technique in modern agile development !!! CS, AU Henrik Bærbak Christensen 27

  28. Note • Please note that once again the 3-1-2 is the underlying and powerful engine for Test Stub . • I use the 3-1-2 to derive a solution that “accidentally” has a name and is a well known concept; just as I previously derived several design patterns. CS, AU Henrik Bærbak Christensen 28

  29. Reusing the variability points... Aah – I could do this... CS, AU Henrik Bærbak Christensen 29

  30. Variability points to the rescue • The WeekendDecisionStrategy introduces yet another variability point... • Often they come in handy later if – 1) they encapsulate well-defined responsibilities – 2) are defined by interfaces and – 3) uses delegation ☺ CS, AU Henrik Bærbak Christensen 30

  31. Static Architecture View CS, AU Henrik Bærbak Christensen 31

  32. Manual testing • Manual testing of GammaTown, for demo to end users! CS, AU Henrik Bærbak Christensen 32

  33. Discussion CS, AU Henrik Bærbak Christensen 33

  34. Test Doubles • Test Stub is a subtype of Test Double. Other sub types exists: – Stub : Get indirect input under control – Spy : Get indirect output under control Return to it shortly • to validate that UUT use the proper protocol – count method calls, ensure proper call sequence, validate set values, – Mock : A spy with fail fast property • Frameworks exists that test code can ‘program’ mocks without every coding them in the native language • Fail fast: fail on first breaking of protocol – Fake : A lightweight but realistic double • when the UUT-DOU interaction is slow and tedious • when the Double interaction is not the purpose of test Source: http://xunitpatterns.com/, G. Meszaros CS, AU Henrik Bærbak Christensen 34

  35. Package/Namespace View • Gradle dictate that we split the code into two trees – src/main/java: all production code rooted here – src/test/java: all test code rooted here • Here – WeekendDecisionStrategy (interface) – ClockBasedDecisionStrategy (class) – FixedDecisionStrategy (class) • Exercise: Where would you put these units? CS, AU Henrik Bærbak Christensen 35

  36. C# Delegates / Java 8 Lambda • The strategy only contains a single method and having an interface may seem a bit of an overkill. – In Java 8, you can use a Lambda – In C# you may use delegates that is more or less a type safe function pointer. – In functional languages you may use closures CS, AU Henrik Bærbak Christensen 36

  37. Test Spy Missing in the FRS book But – they are in the exam set…

  38. Direct versus Indirect • But – there is also output from UUTs Direct input Indirect input Direct output Indirect output CS, AU Henrik Bærbak Christensen 38

  39. Example • UUT: Algorithm to turn on heating in home, based upon measuring the room’s temperature – if (sensor.measureTemperature() < 20) heater.turnOn(); • Test case in JUnit – Given a HeatingController – When the temperature falls to 19 degrees – Then the heater should be turned on CS@AU Henrik Bærbak Christensen 39

  40. Code View Issue: I must validate that ‘turnOn()’ is called by our UUT = HeatingController. But how? It is indirect output from UUT CS@AU Henrik Bærbak Christensen 40

  41. Exercise: Spend 2 minutes Issue: I must validate that ‘turnOn()’ is called by our UUT = HeatingController. But how? It is indirect output from UUT CS@AU Henrik Bærbak Christensen 41

  42. Solution • Is a Test Spy – A ‘recorder’ which records the indirect output sent out by the UUT • … whose recorded values can be asserted… CS@AU Henrik Bærbak Christensen 42

  43. Solution • Spies often need to have specialized retrieval interfaces – i.e. methods that are not in the normal interface, only in the implementing Spy class • To get access to the recorded values • So you have to instantiate the spy by the class, not by the interface type… – Now ‘isTurnedOn()’ is accessible… CS@AU Henrik Bærbak Christensen 43

  44. Summary

  45. Key Points • Test Stubs (Doubles) make software testable. • 3-1-2 technique help isolating DOUs – because I isolated the responsibility by an interface I had the opportunity to delegate to a test stub • My solution is overly complex – Yes! Perhaps subclassing in test tree would be better here ☺ – But • it scales well to complex DOUs • it is good at handling aspects that may vary across the entire system (see next slide) CS, AU Henrik Bærbak Christensen 45

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend