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);
}
}
}