Discussion:
Howto mock a class with constructor parameters
Trond Andersen
2005-09-13 13:55:10 UTC
Permalink
This has probably been described earlier, but I couldn't find any
information about this on

http://jmock.codehaus.org/cglib.html

I see that the JavaDocs has a reference to CGLIBCoreMock and a
constructor taking Class and Object array as parameters, but I'm using
the jmock-1.0.1 release and I can't find any such constructors.

So how do I specify constructor parameters when mocking a class? Can I
use the InvocationDispatcher parameter to address these requirements?
If so - how?

My current JUnit tests throws the following exception:=20

Superclass has no null constructors but no arguments were given

java.lang.IllegalArgumentException: Superclass has no null
constructors but no arguments were given
at net.sf.cglib.proxy.Enhancer.emitConstructors(Enhancer.java:712)
at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:499)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStra=
tegy.java:25)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.j=
ava:215)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:654)
at org.jmock.cglib.CGLIBCoreMock.<init>(Unknown Source)
at org.jmock.cglib.CGLIBCoreMock.<init>(Unknown Source)

Thanks
Nat Pryce
2005-09-13 16:01:48 UTC
Permalink
It's implemented in the latest snapshot but not in 1.0.1, sorry.

As a workaround you can create a subclass with a no-arg constructor
and mock that.

E.g.

public class Orange {
public Orange(int numberOfSegments) ...

public Segment[] peel() ...
}


public class MockableOrange extends Orange
public MockableOrange() {
this(0);
}
}

public class JuicerTest extends MockObjectTestCase {
Mock orange = mock(MockableOrange.class,"orange");

...
}

--Nat.
Post by Trond Andersen
This has probably been described earlier, but I couldn't find any
information about this on
http://jmock.codehaus.org/cglib.html
I see that the JavaDocs has a reference to CGLIBCoreMock and a
constructor taking Class and Object array as parameters, but I'm using
the jmock-1.0.1 release and I can't find any such constructors.
So how do I specify constructor parameters when mocking a class? Can I
use the InvocationDispatcher parameter to address these requirements?
If so - how?
My current JUnit tests throws the following exception:=20
Superclass has no null constructors but no arguments were given
java.lang.IllegalArgumentException: Superclass has no null
constructors but no arguments were given
at net.sf.cglib.proxy.Enhancer.emitConstructors(Enhancer.java:712)
at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:499)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStra=
tegy.java:25)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.j=
ava:215)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:654)
at org.jmock.cglib.CGLIBCoreMock.<init>(Unknown Source)
at org.jmock.cglib.CGLIBCoreMock.<init>(Unknown Source)
Thanks
Loading...