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

Optionally generate Client Stubs #1798

Open
octonato opened this issue Jun 15, 2023 · 3 comments
Open

Optionally generate Client Stubs #1798

octonato opened this issue Jun 15, 2023 · 3 comments
Labels
codegen enhancement New feature or request

Comments

@octonato
Copy link
Member

The idea is to let the codegen generates clients stubs for tests freeing users from defining it by hand. Especially useful for services with many many methods.

There are two types of stubs that can be generated. One that throws exceptions and one that always returns the default instance of the return message.

object SomeApiClientStub {

  trait Unimplemented extends SomeApiClient {
    def someRpc(in: SomeRpcRequest): Future[SomeRpcResponse] = ???
  }

  trait Empty extends SomeApiClient {
    def someRpc(in: SomeRpcRequest): Future[SomeRpcResponse] = Future.successful(SomeRpcResponse.defaultInstance)
}

The users could then override only the methods they are interested in for their test leaving the others simply untouched.

@johanandren
Copy link
Member

Throwing sounds like the path to go to me. I can't think of any benefits from returning an empty default response message in a test scenario?

@johanandren johanandren added codegen java enhancement New feature or request and removed java labels Jun 15, 2023
@alexklibisz
Copy link
Contributor

I opened the original Lightbend support ticket requesting this. Thanks for opening here.

I think just throwing is fine. I'm trying to think why we used the Empty pattern, and can't remember a compelling reason. I just tried replacing Empty with Unimplemented in a couple places and it works fine.

One more consideration: in some cases we've also found it useful to have an object and/or method as follows:

object SomeApiClientStub {

  trait Unimplemented extends SomeApiClient {
    def someRpc(in: SomeRpcRequest): Future[SomeRpcResponse] = ???
  }

  object Unimplemented extends Unimplemented

  def unimplemented: SomeApiClient = new Unimplemented
}

This will be extremely helpful for reducing boilerplate in test suites. Thanks :)

@alexklibisz
Copy link
Contributor

It might also be useful to throw a custom exception, roughly, final class AkkaGrpcStubNotImplementedException extends NotImplementedError, to make it more obvious that a stubbable, unimplemented method was called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
codegen enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants