google / protobuf-gradle-plugin

Protobuf Plugin for Gradle
Other
1.77k stars 273 forks source link

How to auto-generate mock code for gRPC using the protoc-gen-grpc-java /-kotlin plugin? #476

Closed 0xbeefbeef closed 3 years ago

0xbeefbeef commented 3 years ago

I am using the following plugins in my build.gradle:

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.14.0'
    }
    plugins {
        java {
        }
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.35.0'
        }
        grpckt {
            artifact = "io.grpc:protoc-gen-grpc-kotlin:1.0.0:jdk7@jar"
        }
    }
...

gRPC and protobuf generation works as expected, however, I would like to auto-generate mocking code as explained in grpc unit testing. I tried to add the option 'generate_mock_code=true' in the task.plugins { sections, both in grpc and grpckt. However, that doesn't seem to work. Can anyone give me a hint how to trigger the grpc mock autogeneration from the protobuf-gradle-plugin? Any help is very much appreciated!

voidzcy commented 3 years ago

gRPC Java and Kotlin do not have mock Stub, that's something only exists in C++. The recommended way for testing gRPC-Java/Kotlin is to use the Inprocess transport. It is using much of the "real" code and its behavior closely matches a real transport. You can see its usage in gRPC's example.

0xbeefbeef commented 3 years ago

Okay, thank you for your help. I will have a look at that.