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
Stub classes shouldn't be final to allow mocking #2160
Comments
It was never our intention to allow mocking the stubs. Any time Mockito does its hacks to mock classes that shouldn't be mocked (like if they only have private constructors, like AbstractStub) it opens the door to us breaking your test. We can't support tests that use Mockito to avoid API restrictions. #1469 (comment) has some specific issues with mocking the stub. Instead of mocking the stub, we would suggest you to mock a service implementation and use InProcess transport for connecting the two. If you use If there are use cases that are poorly supported or utilities that could be useful (like #2051), we'd be happy to hear about them. |
This thread is somewhat dated so not sure if info is upto but cant StubInterface be used for mocking ? |
@srivastavag, there isn't a |
Oops, I was looking for c++, extremely sorry for my oversight. |
@ejona86 All the linked examples assume you mock the service you are testing, which sounds weird to me (see https://github.com/grpc/grpc-java/blob/master/examples/src/test/java/io/grpc/examples/helloworld/HelloWorldClientTest.java) I understand your suggestion to mock the impl instead, but I am not sure how to actually make this work. I have this code: @Module
public class XyzTestModule {
@Provides
BindableService provideXzyGrpcService(Datastore datastore) {
GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor();
AbcGrpc.AbcImplBase abcImpl =
mock(AbcGrpc.AbcImplBase.class, delegatesTo(new AbcGrpc.AbcImplBase() {}));
grpcServerRule.getServiceRegistry().addService(abcImpl);
AbcGrpc.AbcBlockingStub abcClient = AbcGrpc.newBlockingStub(grpcServerRule.getChannel());
return new XyzService(datastore, abcClient);
}
} but get NullPointerExceptions for It looks like I'll have to replicate most of what is in https://github.com/grpc/grpc-java/blob/master/testing/src/main/java/io/grpc/testing/GrpcServerRule.java, right? |
#2077 Seems to have made stub classes final. As stubs do remote calls, it's usually important for them to be mockable to write unit tests, but mockito can't mock final classes. Previously users could use the interface I guess. While I understand the reasoning of dropping the interface for service classes in #1469, to allow stub upgrades without breaking service implementation code compile, does this apply to client stubs too? It'd be nice for the stub classes to either be non-final or have interfaces to allow mocking.
The text was updated successfully, but these errors were encountered: