Is it possible to mock a Java protocol buffer message?

Viewed 4868

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.

2 Answers

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.

JDave has an Unfinalizer that integrates with the JMock ClassImposteriser

It can't unfinalize classes loaded from the the boot classloader, and it requires a VM argument when launching the tests.

Related