Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

    View Slide

  2. Gopal S Akshintala


    Engineering Lead, Salesforce
    Community
    Day 2022
    September 10th, 2022


    T-Hub, Hyderabad
    @GopalAkshintala
    overfullstack.ga

    View Slide

  3. Sachin Tendulkar


    holds the record for


    highest number of dismissals


    in the 90s


    (28 times)🏏

    View Slide

  4. @GopalAkshintala

    View Slide

  5. Why


    Unit Test ?


    🧪
    @GopalAkshintala

    View Slide

  6. Why


    Unit Test?
    @GopalAkshintala
    Find Bugs
    Statement Coverage
    Behaviour documentation in form
    of code
    Strive for behaviour coverage
    NOT Statement coverage

    View Slide

  7. What makes Unit Testing


    Di
    ffi
    cult? 😓
    @GopalAkshintala
    Code Smells
    void methods
    static methods that are not static


    Bad Design
    Legacy Code interactions

    View Slide

  8. We have the Hammer!
    OOps!
    @GopalAkshintala

    View Slide

  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

    View Slide

  10. The PowerMock Rocky!
    @GopalAkshintala
    OOps!
    100%

    View Slide

  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

    View Slide

  12. @GopalAkshintala

    View Slide

  13. PowerMock


    is a 💊


    Painkiller
    @GopalAkshintala

    View Slide

  14. But! Why is


    PowerMock


    Invasive?
    @GopalAkshintala

    View Slide

  15. @GopalAkshintala
    powermock.github.io

    View Slide

  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

    View Slide

  17. But do I need Unit Tests at all?


    Can’t I get Coverage with


    Integration Tests!?


    🤷
    @GopalAkshintala

    View Slide

  18. @GopalAkshintala
    ITests Vs UTests

    View Slide

  19. @GopalAkshintala
    Use-Case


    Diagram

    View Slide

  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

    View Slide

  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

    View Slide

  22. The Redemption


    🍕 🥗
    @GopalAkshintala

    View Slide

  23. @GopalAkshintala
    Code Smells


    🐽

    View Slide

  24. @GopalAkshintala
    Code Smells 🐽
    Avoid
    void
    Methods

    View Slide

  25. @GopalAkshintala
    Code
    Smells


    🐽
    Are static methods


    bad


    for Unit testing?

    View Slide

  26. @GopalAkshintala
    Pure Vs Impure
    Pure


    Logic
    Impure


    Side-Effects
    REST I/O


    DB I/O


    Events
    • Data-in

    Data-out


    • Referentially
    Transparent

    View Slide

  27. @GopalAkshintala
    Impure
    static void add(int a, int b) {


    System.out.println(a + b);


    }

    View Slide

  28. @GopalAkshintala
    Pure
    static int add(int a, int b) {


    return a + b;


    }

    View Slide

  29. @GopalAkshintala
    Impure Pure
    static void add(int a, int b,


    Consumer 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));

    View Slide

  30. @GopalAkshintala
    Bad Design

    View Slide

  31. @GopalAkshintala
    Bad Design
    OOP
    P
    💩

    View Slide

  32. @GopalAkshintala
    👨💼
    🙉
    Domain


    Logic
    DB


    layer
    Prod
    Test

    View Slide

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

    View Slide

  34. @GopalAkshintala
    Test in
    Isolation
    Mock
    Dependencies
    NOT
    Statements

    View Slide

  35. @GopalAkshintala
    Test in
    Isolation
    Inject
    your
    Dependencies
    💉

    View Slide

  36. @GopalAkshintala
    Legacy


    Dependencies
    🗿
    🗿

    View Slide

  37. @GopalAkshintala
    Legacy
    Dependencies
    If you interact
    with a

    Legacy component,
    Your component
    Instantly turns Legacy!

    View Slide

  38. @GopalAkshintala
    Legacy
    Dependencies
    If you “Directly” interact
    with a

    Legacy component,
    Your component
    Instantly turns Legacy!

    View Slide

  39. @GopalAkshintala
    Ports & Adapters
    Domain
    Adapters

    View Slide

  40. @GopalAkshintala
    👨💼
    🙉
    Domain


    Logic
    DB


    layer
    Prod
    Test

    View Slide

  41. @GopalAkshintala
    Port
    👨💼
    🐵
    Isolated


    Domain


    Logic
    DB


    layer
    Prod Adapter
    Fake Adapter
    Prod
    Test

    View Slide

  42. @GopalAkshintala
    Black box Unit Testing
    Unit Test
    !
    =
    Test Internals


    Unit Test
    ==
    E2E test for the Component

    View Slide

  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

    View Slide

  44. No one gives you Change,
    you need to bring Change
    - Ramulu, Bus conductor
    #dadJoke

    View Slide

  45. @GopalAkshintala
    github.com/overfullstack/sttc-demo
    { }
    speakerdeck.com/gopalakshintala/
    score-the-tendulkar-test-coverage

    View Slide

  46. @GopalAkshintala
    🐵

    View Slide