{"action":"closed","issue":{"url":"https://api.github.com/repos/grpc/grpc-java/issues/4171","repository_url":"https://api.github.com/repos/grpc/grpc-java","labels_url":"https://api.github.com/repos/grpc/grpc-java/issues/4171/labels{/name}","comments_url":"https://api.github.com/repos/grpc/grpc-java/issues/4171/comments","events_url":"https://api.github.com/repos/grpc/grpc-java/issues/4171/events","html_url":"https://github.com/grpc/grpc-java/issues/4171","id":302298942,"number":4171,"title":"Make GrpcServerRule more reusable","user":{"login":"ensonic","id":1279486,"avatar_url":"https://avatars2.githubusercontent.com/u/1279486?v=4","gravatar_id":"","url":"https://api.github.com/users/ensonic","html_url":"https://github.com/ensonic","followers_url":"https://api.github.com/users/ensonic/followers","following_url":"https://api.github.com/users/ensonic/following{/other_user}","gists_url":"https://api.github.com/users/ensonic/gists{/gist_id}","starred_url":"https://api.github.com/users/ensonic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ensonic/subscriptions","organizations_url":"https://api.github.com/users/ensonic/orgs","repos_url":"https://api.github.com/users/ensonic/repos","events_url":"https://api.github.com/users/ensonic/events{/privacy}","received_events_url":"https://api.github.com/users/ensonic/received_events","type":"User","site_admin":false},"labels":[{"id":155505170,"url":"https://api.github.com/repos/grpc/grpc-java/labels/question","name":"question","color":"cc317c","default":true}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-03-05T13:26:23Z","updated_at":"2018-03-08T21:49:52Z","closed_at":"2018-03-08T21:49:52Z","author_association":"NONE","body":"See #2160 for some context. All the linked examples assume one mocks the service that is tested, which sounds weird to me (see https://github.com/grpc/grpc-java/blob/master/examples/src/test/java/io/grpc/examples/helloworld/HelloWorldClientTest.java)\r\nMy grpc service talks to other grpc services and I'd like to mock out those. My Dagger module injects those secondary grpc service stubs and hence in the Test dagger module I'll need to provide mocked stubs and this is when things seem to fall apart.\r\n\r\nI understand the suggestion to mock the impl instead (again see #2160), but I am not sure how to actually make this work. I have this code:\r\n``` java\r\n@Module\r\npublic class XyzTestModule {\r\n    protected static final AbcGrpc.AbcImplBase abcImpl =\r\n      mock(AbcGrpc.AbcImplBase.class, delegatesTo(new AbcGrpc.AbcImplBase() {}));\r\n\r\n  @Provides\r\n  BindableService provideXzyGrpcService(Datastore datastore) {\r\n    GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor();\r\n    // FIXME: need to somehow invoke before(), but this is a hack\r\n    try {\r\n      Method before = grpcServerRule.getClass().getDeclaredMethod(\"before\");\r\n      before.setAccessible(true);\r\n      before.invoke(grpcServerRule);\r\n    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {\r\n      e.printStackTrace();\r\n    }\r\n    grpcServerRule.getServiceRegistry().addService(abcImpl);\r\n    AbcGrpc.AbcBlockingStub abcClient =  AbcGrpc.newBlockingStub(grpcServerRule.getChannel());\r\n\r\n    return new XyzService(datastore, abcClient);\r\n  }\r\n}\r\n```\r\nbur of course the use of reflection is a terrible hack. 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? So maybe the code in GrpcServerRule can be made more resuable. E.g. Datastore has a LocalDatastoreHelper, where I can start()/stop() in my tests @BeforeClass/@AfterClass helpers."}}
{"actor":{"display_login":"ejona86"}}