Thursday, 14 June 2007

JMockit : Non-public mock class

Running a simple unit test, with the 'new Object()' syntax.


public void test()
{
Mockit.redefineMethods(emeraldjava.impl.ApplicationCase.class, new Object() {
public void method()
{
System.out.println("JMocked");
}
});
}
ApplicationCaseTest appCase = new ApplicationCaseTest(getName());
assert(appCase.method(),..);
}


I got this error.


java.lang.IllegalArgumentException: Non-public mock class
at mockit.Mockit.collectMockMethods(Mockit.java:182)
at mockit.Mockit.redefineMethods(Mockit.java:170)
at mockit.Mockit.redefineMethods(Mockit.java:124)
at emerabldjava.workflow.ClaimIntakeTest.test2(ClaimIntakeTest.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:166)
at junit.framework.TestCase.runBare(TestCase.java:140)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:131)
at junit.framework.TestSuite.runTest(TestSuite.java:173)
at junit.framework.TestSuite.run(TestSuite.java:168)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


Solution

Use an named class


public Test extends TestCase
{

public class AppMock
{
public void method()
{
System.out.println("JMocked");
}
}

public void test()
throws AppException, InformationalException, ClassNotFoundException
{
Mockit.redefineMethods(emeraldjava.impl.ApplicationCase.class, new AppMock());
UICApplicationCaseTest appCase = new UICApplicationCaseTest(getName());
assert(appCase.method(),..);
}
}

No comments: