views:

254

answers:

2

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.

+1  A: 

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.

flamingpenguin
+2  A: 

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.

aberrant80
JMockit fails to mock them with a java.lang.ExceptionInitializerError.
cdleary
Did you try all the javaagent redefinition thingy? If you're trying to mock a system/core java class, you need to override the JVM boot classpath. Maybe your tests is missing an annotation? Sorry I can't be of more help. I'm not familiar with the class you're trying to mock or how you're approaching it.
aberrant80
Actually, it should be easy to get a JMockit test working: either add `-javaagent:<proper path>/jmockit.jar` as a JVM startup parameter, or add jmockit.jar before junit-xyz.jar to the classpath if using JDK 1.6. If it still fails, post your specific problem in the JMockit Users discussion group.
Rogerio