$30 off During Our Annual Pro Sale. View Details »

Score the Tendulkar Test Coverage

Score the Tendulkar Test Coverage

Simple coding principles and design patterns you can follow to make your code testable and achieve organic and high test coverage

Gopal S Akshintala

September 10, 2022
Tweet

More Decks by Gopal S Akshintala

Other Decks in Programming

Transcript

  1. Score the Tendulkar Test Coverage @GopalAkshintala

  2. Gopal S Akshintala Engineering Lead, Salesforce Community Day 2022 September

    10th, 2022 T-Hub, Hyderabad @GopalAkshintala overfullstack.ga
  3. Sachin Tendulkar holds the record for highest number of dismissals

    in the 90s (28 times)🏏
  4. @GopalAkshintala

  5. Why Unit Test ? 🧪 @GopalAkshintala

  6. Why Unit Test? @GopalAkshintala Find Bugs Statement Coverage Behaviour documentation

    in form of code Strive for behaviour coverage NOT Statement coverage
  7. What makes Unit Testing Di ffi cult? 😓 @GopalAkshintala Code

    Smells void methods static methods that are not static Bad Design Legacy Code interactions
  8. We have the Hammer! OOps! @GopalAkshintala

  9. The Unstoppable Hammer! OOps! @GopalAkshintala Suppress this and super constructor

    static init blocks Stub private methods static methods final methods Constructors Mock final classes
  10. The PowerMock Rocky! @GopalAkshintala OOps! 100%

  11. The Dark Powers 👹 Synthetic coverage is dangerous PowerMock is

    a new framework to learn Involves a lot of Setup and trail-and-error to get it right Tests written with PowerMock are brittle and difficult to Maintain It’s not Transparent OOps! @GopalAkshintala
  12. @GopalAkshintala

  13. PowerMock is a 💊 Painkiller @GopalAkshintala

  14. But! Why is PowerMock Invasive? @GopalAkshintala

  15. @GopalAkshintala powermock.github.io

  16. PowerMock Compatibility Incompatible with JUnit 5 JUnit 5 was release

    5 years ago Incompatible with Latest Mockito Latest Mockito: 4.20 Compatible version: 3.12 Not fully compatible with Java 11 OOps! @GopalAkshintala
  17. But do I need Unit Tests at all? Can’t I

    get Coverage with Integration Tests!? 🤷 @GopalAkshintala
  18. @GopalAkshintala ITests Vs UTests

  19. @GopalAkshintala Use-Case Diagram

  20. @GopalAkshintala ITests Vs UTests Use-case: How customer uses ITests should

    test Features Use-cases : Features : Components 1 Feature: Reflects software Functionality UTests should test Component Behaviour N N
  21. @GopalAkshintala ITests Vs UTests Traceability - How well a given

    test can precisely trace/pinpoint the cause of failure when it fails Productivity - Writing, Executing and Maintaining effort
  22. The Redemption 🍕 🥗 @GopalAkshintala

  23. @GopalAkshintala Code Smells 🐽

  24. @GopalAkshintala Code Smells 🐽 Avoid void Methods

  25. @GopalAkshintala Code Smells 🐽 Are static methods bad for Unit

    testing?
  26. @GopalAkshintala Pure Vs Impure Pure Logic Impure Side-Effects REST I/O

    DB I/O Events • Data-in 
 Data-out • Referentially Transparent
  27. @GopalAkshintala Impure static void add(int a, int b) { System.out.println(a

    + b); }
  28. @GopalAkshintala Pure static int add(int a, int b) { return

    a + b; }
  29. @GopalAkshintala Impure Pure static void add(int a, int b, Consumer<Integer>

    consumer) { consumer.accept(a + b); } / / Function call in Prod add(1, 2, System.out :: println); / / Function call in Test add(1, 2, result -> assertEquals(3, result));
  30. @GopalAkshintala Bad Design

  31. @GopalAkshintala Bad Design OOP P 💩

  32. @GopalAkshintala 👨💼 🙉 Domain Logic DB layer Prod Test

  33. @GopalAkshintala Design for Tests A Unit Test is the First

    client for your Component
  34. @GopalAkshintala Test in Isolation Mock Dependencies NOT Statements

  35. @GopalAkshintala Test in Isolation Inject your Dependencies 💉

  36. @GopalAkshintala Legacy Dependencies 🗿 🗿

  37. @GopalAkshintala Legacy Dependencies If you interact with a
 Legacy component,

    Your component Instantly turns Legacy!
  38. @GopalAkshintala Legacy Dependencies If you “Directly” interact with a
 Legacy

    component, Your component Instantly turns Legacy!
  39. @GopalAkshintala Ports & Adapters Domain Adapters

  40. @GopalAkshintala 👨💼 🙉 Domain Logic DB layer Prod Test

  41. @GopalAkshintala Port 👨💼 🐵 Isolated Domain Logic DB layer Prod

    Adapter Fake Adapter Prod Test
  42. @GopalAkshintala Black box Unit Testing Unit Test ! = Test

    Internals Unit Test == E2E test for the Component
  43. @GopalAkshintala Anti-Patterns (digitaltapestry.net) https://confluence.internal.salesforce.com/display/TESTSFDC/ Unit+Testing+Best+Practices https://confluence.internal.salesforce.com/display/TESTSFDC/ Unit+Test+Recipes%3A+How+to+Write+High-Quality+Tests Explanation how proxy

    based Mock Frameworks work Write tests. Not too many. Mostly integration. (kentcdodds.com) Testing of Microservices - Spotify Engineering : Spotify Engineering (atspotify.com) On the Diverse And Fantastical Shapes of Testing (martinfowler.com) ongoing by Tim Bray · Testing in the Twenties (tbray.org) Resources
  44. No one gives you Change, you need to bring Change

    - Ramulu, Bus conductor #dadJoke
  45. @GopalAkshintala github.com/overfullstack/sttc-demo { } speakerdeck.com/gopalakshintala/ score-the-tendulkar-test-coverage

  46. @GopalAkshintala 🐵