To provide a bit of context, I needed to mock the following Java class:
javax.security.auth.kerberos.KerberosPrincipal
I worked around the issue by using the Adapter pattern (then mocking
KerberosPrincipalAdapter, rather than KerberosPrincipal), but I'm interested
to know what approaches other people use in relation to Spock.
--
View this message in context: http://spock-framework.3207229.n2.nabble.com/Mocking-final-classes-tp6309434p6309434.html
Sent from the Spock Framework mailing list archive at Nabble.com.
I tried adding PowerMock (Mockito API) to my Spock test, but am getting a
"too much data" exception and am interested to know if anyone else has
encountered this and/or has managed to get PowerMock working with Spock?
I have something along the following lines:
....
import static org.mockito.Mockito.*
import javax.security.auth.kerberos.KerberosPrincipal;
import spock.lang.*
import org.junit.Rule
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.rule.PowerMockRule
...
@PrepareForTest(KerberosPrincipal.class)
class MyTestSpec extends Specification {
@Rule
PowerMockRule rule = new PowerMockRule()
def "some test"() {
when: "some stimulus"
def principal =
mock(KerberosPrincipal.class)
then: "some output"
}
}
I think I have all the relevant PowerMock libraries included locally (as
well as a Maven reference to "xstream"):
objenesis-1.2.jar
javassist-3.14.0-GA.jar
cglib-nodep-2.2.jar
mockito-all-1.8.5.jar
powermock-classloading-xstream-1.4.8.jar
powermock-mockito-1.4.8-full.jar
However, I'm seeing the following exception when the test is executed:
java.lang.RuntimeException: javassist.CannotCompileException: by
java.io.IOException: too much data
at
org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:188)
at
org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:148)
at
org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:65)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at ...
Caused by: javassist.CannotCompileException: by java.io.IOException: too
much data
at javassist.CtClassType.toBytecode(CtClassType.java:1438)
at javassist.CtClass.toBytecode(CtClass.java:1279)
at
org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:185)
... 4 more
Caused by: java.io.IOException: too much data
at javassist.ClassPoolTail.copyStream(ClassPoolTail.java:440)
at javassist.ClassPoolTail.writeClassfile(ClassPoolTail.java:290)
at javassist.ClassPool.writeClassfile(ClassPool.java:637)
at javassist.CtClassType.toBytecode(CtClassType.java:1426)
... 6 more
This seems related to the following PowerMock issues:
http://code.google.com/p/powermock/issues/detail?id=235
http://code.google.com/p/powermock/issues/detail?id=290
My key question is: has anyone managed to get PowerMock working with Spock?
If so, how?
Many thanks!
--
View this message in context: http://spock-framework.3207229.n2.nabble.com/Mocking-final-classes-tp6309434p6312703.html
> This seems related to the following PowerMock issues:
>
> http://code.google.com/p/powermock/issues/detail?id=235
> http://code.google.com/p/powermock/issues/detail?id=290
>
> My key question is: has anyone managed to get PowerMock working with Spock?
> If so, how?
These issues seem to indicate that PowerMock doesn't currently work with test classes written in Groovy, which obviously means that it won't work with Spock. Apparently PowerMock not only manipulates the classes to be mocked, but also the test classes.
Cheers,
Peter