jmock-developers / jmock-library

An expressive Mock Object library for Test Driven Development
http://www.jmock.org
BSD 3-Clause "New" or "Revised" License
133 stars 70 forks source link

JMock Library

Maven Build Maven Central

Maven

  <dependency>
    <groupId>org.jmock</groupId>
    <artifactId>jmock-junit5</artifactId>
    <version>2.13.0</version>
    <scope>test</scope>
  </dependency>

Gradle

Gradle 7 and Above

testImplementation(
    "junit:junit5:5.3.1",
    "org.jmock:jmock-junit5:2.13.0"
)

Gradle 6 and Below

testCompile(
    "junit:junit5:5.3.1",
    "org.jmock:jmock-junit5:2.13.0"
)

Recent Changes

JUnit 5 Support


import org.jmock.Expectations;
import org.jmock.junit5.JUnit5Mockery;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class JUnit5TestThatDoesSatisfyExpectations {
    @RegisterExtension
    JUnit5Mockery context = new JUnit5Mockery();
    private Runnable runnable = context.mock(Runnable.class);

    @Test
    public void doesSatisfyExpectations() {
        context.checking(new Expectations() {{
            oneOf (runnable).run();
        }});

        runnable.run();
    }
}

JUnit 4 moved to provided scope in org.jmock:jmock

Advantages of jMock 2 over jMock 1

Package Structure

[jMock]() 2 is organised into published and internal packages. We guarantee backwards compatability of types in published packages within the same major version of jMock. There are no guarantees about backward compatability for types in internal packages.

Types defined in published packages may themselves define public methods that accept or return types from internal packages or inherit methods from types in internal packages. Such methods have no compatability guarantees and should not be considered as part of the published interface.

Published packages

org.jmock

DSL-style API

org.jmock.api

org.jmock.lib

Convenient classes that implement the APIs in the core, are used by the DSL-style API, and can be used in user-defined APIs

org.jmock.integration

Classes integrating jMock with different testing APIs, such as JUnit 3.x, JUnit 4.x and TestNG.

Packages of example code

org.jmock.lib.nonstd

Lib classes that rely on clever hacks or otherwise cannot be guaranteed to always work in all JVMs. There are no compatability guarantees with these classes. Use at your own risk.

Internal packages

org.jmock.internal

Internal implementation details

org.jmock.test

Tests for jMock itself

Plug-in Points

Matcher

Controls the matching of invocations to expectations

Action

Performs an action in response to an invocation

Imposteriser

Wraps mock objects in an adapter of the correct type

Expectation

Matches an invocation and fakes its behaviour

ExpectationErrorTranslator

Translates expectation errors into error type used by a specific testing framework.

MockObjectNamingScheme

Creates names for mock objects based on the mocked type.

Contributing

If you'd like to contribute, then do the following:

  1. clone this repository (git clone …)
  2. install Maven (brew install mvn on Mac OS, for example)
  3. $ mvn package in order to generate a signed JAR. This will run all the tests. ($ mvn test appears not to suffice.)