Mocking final classes?

2,483 views
Skip to first unread message

Duncan Sommerville

unread,
Apr 27, 2011, 9:19:03 AM4/27/11
to spockfr...@googlegroups.com
I realise that Spock is unable to mock final classes at present, but I'm
interested to know if there are any plans to support this in future and/or
what the generally accepted solution is.

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.

Mathilde Lemée

unread,
Apr 27, 2011, 9:37:55 AM4/27/11
to spockfr...@googlegroups.com
Hi,

You can also use powermock to mock final classes but Adapter pattern is a more acceptable option in my opinion.

Mathilde Lemée
@MathildeLemee
http://www.java-freelance.fr

De : Duncan Sommerville <dsomme...@gmail.com>
À : spockfr...@googlegroups.com
Envoyé le : Mer 27 avril 2011, 15h 19min 03s
Objet : Mocking final classes?
--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To post to this group, send email to spockfr...@googlegroups.com.
To unsubscribe from this group, send email to spockframework+unsub...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.

Peter Niederwieser

unread,
Apr 27, 2011, 12:25:49 PM4/27/11
to Spock Framework - User
Spock will support dynamic Groovy mocks at some point, which will
allow you to mock a final class as long as its clients are written in
Groovy. We don't plan to support the same for clients written in Java
because it requires a lot of dark magic. If you really want to go
there, use PowerMock, which should work just fine with Spock. Solving
the problem by adding one layer of abstraction (like you did with the
adapter) is also fine, and more in the spirit of mocking.

Cheers,
Peter

Duncan Sommerville

unread,
Apr 28, 2011, 6:11:11 AM4/28/11
to spockfr...@googlegroups.com
Thanks for the responses on this - I'd like to explore the use of PowerMock
with Spock a little further (while I acknowledge that the Adapter pattern is
generally preferred, the use of PowerMock might be more appropriate in my
current scenario).

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

Peter Niederwieser

unread,
Apr 29, 2011, 11:16:11 AM4/29/11
to spockfr...@googlegroups.com
On 28.04.2011, at 12:11, Duncan Sommerville wrote:

> 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

Paolo Ibarra

unread,
Jul 17, 2014, 9:02:37 AM7/17/14
to spockfr...@googlegroups.com
In case someone stumbles on this thread, I managed to see a repo in github that made Powermock work with Spock. Check this answer in stackoverflow too: http://stackoverflow.com/questions/19493690/using-powermock-with-spock/24749047#24749047
Reply all
Reply to author
Forward
0 new messages