"How do I use Mockito to mock grpc ServiceBlockingStub to throw StatusRuntimeException with a specific status?" - Java developer
You have a few options:
You have a few options:
- NOT RECOMMENDED: Use Mockito v2 to mock final classes and methods.
- NOT RECOMMENDED: Use powermock to mocks final classes and methods.
- Recommended: Use the gRPC Java test framework like GrpcCleanupRule and InProcessServerBuilder. See HelloWorldClientTest for an example.
- Recommended: Do over-the-wire gRPC API mocking/simulation. Use a third-party tool that will create over-the-wire API mocks/simulators for your API. For example, Traffic Parrot.
Note why mocking final, in this case, might be a bad idea: Mocking final classes or methods might be a bad idea, depending on the case. The devil is in the details. In your situation, you are creating a mock of the generated code, so you are assuming how that generated code will behave in the future. gRPC and Protobuf are still rapidly evolving, so it might be risky to make those assumptions, as they might change and you won't notice because you do not check your mocks against the generated code. Hence, it's not a good idea to mock the generated code unless you really have to.
No comments:
Post a Comment