protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
65.37k stars 15.46k forks source link

Repeat: Java generation: allow non-final generation for classes #16793

Closed carojkov closed 1 month ago

carojkov commented 4 months ago

re: https://github.com/protocolbuffers/protobuf/issues/11162

Allow mocking stubs - don't make them final

I'd love to be able to mock stubs

    private HelloServiceGrpc.HelloServiceBlockingStub stub;

    public void hello(String message) {
        stub.hello(message); <-- I should be able to make a unit test that verifies stub.hello() is called and with the right value.
    }

@MarinaShaposhnikova @artnaseef

artnaseef commented 4 months ago

I agree - it would be excellent to be able to mock. The only response I've gotten in the past from google devs (not protobuf, though) as to why the classes are marked final is because they wanted to ensure users of their libraries don't use inheritance on those classes.

That response feels like using a Nuke to remove the tree stump in the back yard to me. However, even that can be solved - using interfaces. Wasn't finding any common ground / interest in working on the matter, so I gave up.

artnaseef commented 4 months ago

As mentioned on #11162 follow-up, this request may belong on a gRPC project instead of this one. Sorry that I don't know the right project to link here myself.

Is this the plugin invloved? https://github.com/xolstice/protobuf-maven-plugin

Looking at the plugin briefly, I did not find references to the grpc/protobuf code generators themselves.

Hope this helps.

carojkov commented 4 months ago

Yep, I think I posted to the wrong project. I'll make sure to re-post to the right one.

However, I think the design issue is systemic - unit tests should be able to mock generated classes. Even Any, Empty etc should be mockable but user classes should be mockable without question.

artnaseef commented 4 months ago

I agree that generated model objects should be inheritable (and, hence, not final), although I can see protobuf folks wanting to be clear that subclasses aren't supported in the marshalling/unmarshalling (aka seralizing/deserializing). At least adding a "I know what I am doing - I swear" option to the generator that drops the final keyword would be nice to have.

@carojkov if you @ me on any new threads, I'll try to help/contribute.

carojkov commented 4 months ago

Thanks, I am sure adding voices makes a differenced.

github-actions[bot] commented 1 month ago

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

github-actions[bot] commented 1 month ago

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.

This issue was closed and archived because there has been no new activity in the 14 days since the inactive label was added.