Skip to content

Clients / Stubs cannot be mocked #2423

Closed

Description

@saturnism

E.g., TraceServiceClient has all of the methods marked as final. Thus it's impossible to use with Mockito. There is no interface to mock either. This makes it hard to test higher level libraries that needs to consume our client/stubs.

Activity

  1. changed the title [-]Stubs cannot be mocked[/-] [+]Clients / Stubs cannot be mocked[/+] on Sep 11, 2017
  2. added
    api: cloudtraceIssues related to the Cloud Trace API.
    priority: p2Moderately-important priority. Fix may not be included in next release.
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    on Sep 14, 2017
  3. shinfan commented on Sep 14, 2017

    @shinfan
    Contributor

    I think this is an issue affecting all auto-gen clients.

    @vam-google What are your thoughts on this?

  4. vam-google commented on Sep 14, 2017

    @vam-google
    Contributor

    Personally I'm not a big fan of final methods and classes. So I'm fine with removing final from our generated clients, and I support the mocking argument. I don't know why it was decided to make a non-final class with all-final methods in it (does it even make sense?).

    Adding an interface to the client so the interface can be mocked is also fine, but it will be more work and more complexity. It will be potentially generating more boilerplate code with no real benefits (the only potential benefit I see here is this will allow to have, for example, 2 different transport implementations for same client: gRPC and, for example, REST).

    @garrettjonesgoogle what is our opinion about it?

  5. added
    priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.
    and removed
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Sep 14, 2017
  6. vam-google commented on Sep 14, 2017

    @vam-google
    Contributor

    I think not allowing to mock our clients is a big issue (mocking networking part is a very often case in unit testing, so our current situation means that everybody who is using our clients will not be able to write proper unit tests for their code). Changing priority from P2+ to P1.

  7. garrettjonesgoogle commented on Sep 20, 2017

    @garrettjonesgoogle
    Contributor

    The reason for final is to reduce the number of places where users could break when we make breaking changes.

    The title of this issue isn't fully correct: There isn't anything final on the stubs. I think the right way to mock is to create a stub, and pass that to the client when constructing it. The client is really just a thin passthrough to the stub anyway.

  8. yihanzhen commented on Jan 18, 2018

    @yihanzhen
    Contributor

    Moving this issue to feature request backlog.

  9. added a commit that references this issue on Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions