Class JUnit5Mockery

java.lang.Object
org.jmock.Mockery
org.jmock.junit5.JUnit5Mockery
All Implemented Interfaces:
org.hamcrest.SelfDescribing, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension

@AutoService(org.junit.jupiter.api.extension.Extension.class) public class JUnit5Mockery extends Mockery implements org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback
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.
  • Field Details

    • mockomatic

      private final Mockomatic mockomatic
  • Constructor Details

    • JUnit5Mockery

      public JUnit5Mockery()
  • Method Details

    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
      Throws:
      Exception
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
      Throws:
      Exception
    • fillInAutoMocks

      private void fillInAutoMocks(Object target, List<Field> allFields)
    • checkMockery

      private static void checkMockery(org.junit.jupiter.api.extension.ExtensionContext context, Class<?> testCaseClass)
    • findMockeryField

      private static Field findMockeryField(Class<?> testClass, org.junit.jupiter.api.extension.ExtensionContext context)