testing final class with easymock and testng

89 views
Skip to first unread message

Atul

unread,
May 19, 2011, 9:05:31 PM5/19/11
to PowerMock
I'm new to PowerMock and can't find an example that matches my
situation.
It's nothing complicated-- I am trying to use PowerMock+EasyMock to
mock a final class in a TestNG test.
A simplified example follows.

Here's the final class:

<code>
public final class MyFinalClass {
private String myString = "someString";

public final String getMyString() {
return myString;
}
}
</code>

The class under test delegates to an instance of the final class:

<code>
public class MyActiveClass {
private MyFinalClass finalClass;

public String getFinalString() {
return finalClass.getMyString();
}

public void setFinalClass(MyFinalClass finalClass) {
this.finalClass = finalClass;
}
}
</code>

And here's the Test class:

<code>
import static org.easymock.EasyMock.expect;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.Assert;
import org.testng.annotations.Test;


@PrepareForTest({ MyFinalClass.class })
public class MyTestClass extends PowerMockTestCase {

private MyActiveClass activeClass = new MyActiveClass();
private MyFinalClass mockFinalClass;

@Test
public void getFinalString() {
String expectedString = "mockedReturnString";
mockFinalClass = PowerMock.createMock(MyFinalClass.class);
activeClass.setFinalClass(mockFinalClass);

expect(mockFinalClass.getMyString()).andReturn(expectedString);
PowerMock.replayAll();
Assert.assertEquals(activeClass.getFinalString(),
expectedString);
PowerMock.verifyAll();
}
}
</code>

When I run the test, I get an illegal argument exception complaining
that MyFinalClass is not an interface:

<code>
FAILED: getFinalString
java.lang.IllegalArgumentException: MyFinalClass is not an interface
at java.lang.reflect.Proxy.getProxyClass(Unknown Source)
at java.lang.reflect.Proxy.newProxyInstance(Unknown Source)
at
org.easymock.internal.JavaProxyFactory.createProxy(JavaProxyFactory.java:
24)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:
51)
at
org.powermock.api.easymock.PowerMock.doCreateMock(PowerMock.java:2212)
at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:
2163)
at org.powermock.api.easymock.PowerMock.createMock(PowerMock.java:
89)
at MyTestClass.getFinalString(MyTestClass.java:22)
...
</code>

What am I missing? Thanks in advance for your help!!!

Johan Haleby

unread,
May 20, 2011, 2:32:53 AM5/20/11
to powe...@googlegroups.com
Hi, 

Hmm the first things that pops up is if the PowerMockObjectFactory is really used?  Or perhaps you have incorrect dependencies in classpath? There's a simple example of mocking final in svn, class under test is here, and it works. Also have a look how that project is configured (suite.xml etc).

/Johan 


--
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.


Reply all
Reply to author
Forward
0 new messages