Showing posts with label jmockit. Show all posts
Showing posts with label jmockit. Show all posts

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(),..);
}
}

Wednesday, 6 June 2007

JMockit

The business class we want to test


package emeraldjava.jmockit;

/**
* Service.
*/
public class Service {

public boolean doBusinessWork()
throws Exception
{
Workflow workflow = new Workflow();
if(workflow.startProcess()==1)
return true;
else
return false;
}

}


The object used by the service class


package emeraldjava.jmockit;

public class Workflow {

public long startProcess()
throws Exception
{
return 1;
}

}


The test class


package curam.uic.tools.jmockit;

import junit.framework.TestCase;
import mockit.Mockit;

public class ServiceTest extends TestCase {

public ServiceTest(String name)
{
super(name);

}

public void test()
throws Exception
{
Service service = new Service();
assertEquals("",true,service.doBusinessWork());
}

public void test2()
throws Exception
{
Service service = new Service();

Mockit.redefineMethods(Workflow.class, new Object() {
public long startProcess()
{
return 2;
}
});
assertEquals("",false,service.doBusinessWork());
}

public static class WorkflowMock
{
public long startProcess()
throws Exception
{
throw new Exception();
}
}

public void test3()
{
Service service = new Service();
Mockit.redefineMethods(Workflow.class, WorkflowMock.class);

try
{
service.doBusinessWork();
}
catch(Exception e)
{
assertNotNull("Exception thrown",e);
}
}
}