Is it possible to mock a Java protocol buffer message?

java, mocking, protocol-buffers

Solution

JMockit can handle `final` and `static`. Just pay attention to how to set it up as it requires the -javaagent JVM parameter, or classpath tweaks, or extra annotations to be able to mock `final` and `static` stuffs.

Problem

Protocol buffer classes are marked `final`, presumably for efficiency; however, this makes them quite difficult to test with -- Mockito can't mock/spy on final classes. I've tried using `PowerMockito` with no success: I get a `ClassFormatError` when preparing the `final` class for the test. My solution up until now is to create mockable adapter interfaces, but I'm hoping there's a less laborious approach.

Original source