Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add annotation to mark a type as DoNotMock #1833

Merged
merged 11 commits into from Nov 19, 2021
Merged

Add annotation to mark a type as DoNotMock #1833

merged 11 commits into from Nov 19, 2021

Commits on Nov 28, 2019

  1. Add annotation to mark a type as DoNotMock

    Mocking types that users not own [1] or are severely complicating test
    logic [2] leads to brittle or wrong tests. In particular, the
    StackOverflow answer is wrong, as the contract of java.util.Map is
    violated. When a new key is added to the Map, the stubbed return would be wrong.
    In Google we have used the DoNotMock annotation via ErrorProne [3]
    to annotate these types, as well as an internal list of types that can't
    be mocked (this includes several java.util types). We are using a custom
    Mockmaker to enforce this on run-time.
    
    Based on our successful experience with DoNotMock (we have seen a large
    reduction in bad/broken tests for types involved), we are proposing to
    open source this into Mockito itself.
    
    The DoNotMock annotation can be added to any type, e.g. classes and
    interfaces. If, in the type hierarchy of the class-to-be-mocked, there
    is a type that is annotated with DoNotMock, Mockito will throw a
    DoNotMockException.
    
    This would help preventing issues such as #1827 and #1734 which is
    in-line with the guidance on our wiki [1]. A follow-up change would
    allow us to define external types (like the java.util types) that can't
    be mocked. (We can't add the annotation to the types, as they live in the
    JDK instead.)
    
    [1]: https://github.com/mockito/mockito/wiki/How-to-write-good-tests#dont-mock-a-type-you-dont-own
    [2]: https://stackoverflow.com/a/15820143
    [3]: https://errorprone.info/api/latest/com/google/errorprone/annotations/DoNotMock.html
    TimvdLippe committed Nov 28, 2019
    Copy the full SHA
    b740f99 View commit details
    Browse the repository at this point in the history

Commits on Dec 2, 2019

  1. Add entry-point for enforcement customization

    This introduces the DoNotMockEnforcer interface which users can override
    to implement their special handling of types annotated with DoNotMock.
    TimvdLippe committed Dec 2, 2019
    Copy the full SHA
    584f6bd View commit details
    Browse the repository at this point in the history
  2. Copy the full SHA
    8119a5d View commit details
    Browse the repository at this point in the history
  3. Rename interface method

    This name is clearer for implementers.
    TimvdLippe committed Dec 2, 2019
    Copy the full SHA
    0257b9b View commit details
    Browse the repository at this point in the history

Commits on Dec 4, 2019

  1. Small review tweaks

    TimvdLippe committed Dec 4, 2019
    Copy the full SHA
    443b6c0 View commit details
    Browse the repository at this point in the history

Commits on Jan 27, 2020

  1. Do not use Java 8 APIs

    They won't compile for Android users
    TimvdLippe committed Jan 27, 2020
    Copy the full SHA
    df129bc View commit details
    Browse the repository at this point in the history

Commits on Jan 28, 2020

  1. Remove unused import

    TimvdLippe committed Jan 28, 2020
    Copy the full SHA
    aa93d90 View commit details
    Browse the repository at this point in the history

Commits on Nov 19, 2021

  1. Copy the full SHA
    0fdb3c3 View commit details
    Browse the repository at this point in the history
  2. Process reviewer feedback

    TimvdLippe committed Nov 19, 2021
    Copy the full SHA
    276abfc View commit details
    Browse the repository at this point in the history
  3. Fix test

    TimvdLippe committed Nov 19, 2021
    Copy the full SHA
    498f2b7 View commit details
    Browse the repository at this point in the history
  4. Remove unused import

    TimvdLippe committed Nov 19, 2021
    Copy the full SHA
    3d5ddb3 View commit details
    Browse the repository at this point in the history