package org.jmock.junit5;
import java.lang.reflect.Field;
import java.util.List;
import org.jmock.Mockery;
import org.jmock.auto.internal.Mockomatic;
import org.jmock.internal.AllDeclaredFields;
import org.jmock.lib.AssertionErrorTranslator;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
import org.junit.jupiter.api.extension.ExtensionContext;
import com.google.auto.service.AutoService;
/**
* A JUnit5Mockery is a JUnit Extension that manages JMock
* expectations and allowances, and asserts that expectations have been met
* after each test has finished. To use it, add a (non-private) field to the test class
* For example,
*
*
* public class ATestWithSatisfiedExpectations {
* @RegisterExtension
* final JUnitRuleMockery context = new JUnitRuleMockery();
*
* @Mock
* private final Runnable runnable;
*
* @Test
* public void doesSatisfyExpectations() {
* context.checking(new Expectations() {
* {
* oneOf(runnable).run();
* }
* });
*
* runnable.run();
* }
* }
*
*
* Note that the Rule field must be declared public and as a
* JUnitRuleMockery (not a Mockery) for JUnit to
* recognise it, as it's checked statically.
*
* @author olibye
*/
@AutoService(Extension.class)
public class JUnit5Mockery extends Mockery
implements Extension, BeforeEachCallback, AfterEachCallback {
private final Mockomatic mockomatic = new Mockomatic(this);
public JUnit5Mockery() {
setExpectationErrorTranslator(AssertionErrorTranslator.INSTANCE);
}
@Override
public void beforeEach(ExtensionContext context) throws Exception {
if (context.getTestClass().isPresent()) {
Class> testCaseClass = context.getTestClass().get();
List