Is it possible to mock a final class with private constructor?

2,469 views
Skip to first unread message

Mishail

unread,
Sep 26, 2010, 10:14:09 PM9/26/10
to PowerMock
I'm trying to mock one class (WebResource.Builder) from Jersey Client
API (https://jersey.dev.java.net/source/browse/jersey/tags/jersey-1.4/
jersey/jersey-client/src/main/java/com/sun/jersey/api/client/
WebResource.java?rev=4215&view=markup).

But I always get "java.lang.IllegalArgumentException: Cannot subclass
final class class"

PowerMock: 1.4.5
EasyMock: 3.0
JUnit: 4.4

....
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.WebResource.Builder;
....

@RunWith(PowerMockRunner.class)
@PrepareForTest({WebResource.class, Builder.class})
.............

@Test
public void getConsoleStatusTest() throws Exception {
.................
Builder bld = PowerMock.createMock(Builder.class);
.................
}


java.lang.IllegalArgumentException: Cannot subclass final class class
com.sun.jersey.api.client.WebResource$Builder
at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
at
net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:
25)
at
net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:
216)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at net.sf.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
at
org.easymock.internal.ClassProxyFactory.createProxy(ClassProxyFactory.java:
181)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:
60)
at org.powermock.api.easymock.PowerMock.doCreateMock(PowerMock.java:
2211)
at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2162)
at org.powermock.api.easymock.PowerMock.createMock(PowerMock.java:98)
at <my test class>

Mishail

unread,
Sep 27, 2010, 4:38:46 AM9/27/10
to PowerMock
So, the more general question - is it possible to mock final inner
classes? (See comments for http://code.google.com/p/powermock/wiki/MockFinal)

Johan Haleby

unread,
Sep 27, 2010, 6:28:00 AM9/27/10
to powe...@googlegroups.com
Hi, 

Not sure about private constructors but I think so. 

Inner-classes are mockable but this is usually something you want to avoid. Here's an example.

/Johan


On Mon, Sep 27, 2010 at 10:38 AM, Mishail <mishail...@gmail.com> wrote:
So, the more general question - is it possible to mock final inner
classes? (See comments for http://code.google.com/p/powermock/wiki/MockFinal)

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.


Mishail

unread,
Sep 27, 2010, 9:20:24 AM9/27/10
to PowerMock
Hi,

I'm afraid that doesn't work when the inner class is final... At least
it doesn't work for me with EasyMock 3.0

On Sep 27, 9:28 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> Not sure about private constructors but I think so.
>
> Inner-classes are mockable but this is usually something you want to avoid.
> Here's an example<http://code.google.com/p/powermock/source/browse/trunk/modules/module...>
> .
>
> /Johan

Mishail

unread,
Sep 27, 2010, 6:35:07 PM9/27/10
to PowerMock
finally I've found the existing issue - http://code.google.com/p/powermock/issues/detail?id=95
thanks anyway for the great tool

Johan Haleby

unread,
Sep 28, 2010, 2:10:00 AM9/28/10
to powe...@googlegroups.com
Yes this was what I expected as well. It's still an open issue for me whether it's allowed or not, Javassist cannot seem to do it though. Perhaps it's a bug or some sort of limitation?! Should bring it up with them.

/Johan

On Tue, Sep 28, 2010 at 12:35 AM, Mishail <mishail...@gmail.com> wrote:
finally I've found the existing issue - http://code.google.com/p/powermock/issues/detail?id=95
thanks anyway for the great tool
Reply all
Reply to author
Forward
0 new messages