Try to use JMockIt.
It has more functions that you can use and also has the mocking algorithm you want.
It is simple to apply.
Example:
package jmockit.tutorial.domain;
import java.math.*;
import java.util.*;
import org.apache.commons.mail.*;
import static jmockit.tutorial.persistence.Database.*;
public final class MyBusinessService
{
public void doBusinessOperationXyz(EntityX data) throws EmailException
{
List<EntityX> items =
(1) find("select item from EntityX item where item.someProperty = ?1", data.getSomeProperty());
// Compute or obtain from another service a total value for the new persistent entity:
BigDecimal total = ...
data.setTotal(total);
(2) persist(data);
sendNotificationEmail(data, items);
}
private void sendNotificationEmail(EntityX data, List<EntityX> items) throws EmailException
{
Email email = new SimpleEmail();
email.setSubject("Notification about processing of ...");
(3) email.addTo(data.getCustomerEmail());
// Other e-mail parameters, such as the host name of the mail server, have defaults defined
// through external configuration.
String message = buildNotificationMessage(items);
email.setMsg(message);
(4) email.send();
}
private String buildNotificationMessage(List<EntityX> items) { ... }
}
Now Using the Expectations API
First, lets use the JMockit Expectations API.
package jmockit.tutorial.domain;
import org.apache.commons.mail.;
import jmockit.tutorial.persistence.;
import org.junit.;
import mockit.;
public final class MyBusinessService_ExpectationsAPI_Test
{
@Mocked(stubOutClassInitialization = true) final Database unused = null;
@Mocked SimpleEmail email;
@Test
public void doBusinessOperationXyz() throws Exception
{
final EntityX data = new EntityX(5, "abc", "abc@xpta.net");
// Recorded strictly, so matching invocations must be replayed in the same order:
new Expectations() {{
(1) Database.find(withSubstring("select"), any);
result = new EntityX(1, "AX5", "someone@somewhere.com");
(2) Database.persist(data);
}};
// Recorded non-strictly, so matching invocations can be replayed in any order:
new NonStrictExpectations() {{
(4) email.send(); times = 1; // a non-strict invocation requires a constraint if expected
}};
new MyBusinessService().doBusinessOperationXyz(data);
}
@Test(expected = EmailException.class)
public void doBusinessOperationXyzWithInvalidEmailAddress() throws Exception
{
new NonStrictExpectations() {{
(3) email.addTo((String) withNotNull()); result = new EmailException();
// If the e-mail address is invalid, sending the message should not be attempted:
email.send(); times = 0;
}};
EntityX data = new EntityX(5, "abc", "someone@somewhere.com");
new MyBusinessService().doBusinessOperationXyz(data);
}
}
Using the Verifications API
package jmockit.tutorial.domain;
import org.apache.commons.mail.*;
import jmockit.tutorial.persistence.*;
import org.junit.*;
import mockit.*;
public final class MyBusinessService_VerificationsAPI_Test
{
@Tested MyBusinessService service; // instantiated automatically
@Mocked(stubOutClassInitialization = true) Database onlyStatics;
@Capturing Email email; // concrete subclass mocked on demand, when loaded
final EntityX data = new EntityX(5, "abc", "someone@somewhere.com");
@Test
public void doBusinessOperationXyzPersistsData() throws Exception
{
// No expectations recorded in this case.
service.doBusinessOperationXyz(data);
(2) new Verifications() {{ Database.persist(data); }};
}
@Test
public void doBusinessOperationXyzFindsItemsAndSendsNotificationEmail() throws Exception
{
// Invocations that produce a result are recorded, but only those we care about.
new NonStrictExpectations() {{
(1) Database.find(withSubstring("select"), (Object[]) null);
result = new EntityX(1, "AX5", "someone@somewhere.com");
}};
service.doBusinessOperationXyz(data);
new VerificationsInOrder() {{
(3) email.addTo(data.getCustomerEmail());
(4) email.send();
}};
}
}
I hope this answers your query.