<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-junit5</artifactId>
<version>2.13.0</version>
<scope>test</scope>
</dependency>
testImplementation(
"junit:junit5:5.3.1",
"org.jmock:jmock-junit5:2.13.0"
)
testCompile(
"junit:junit5:5.3.1",
"org.jmock:jmock-junit5:2.13.0"
)
- Swap @Rule JUnit4Mockery for @RegisterExtension JMock5Mockery
- Assign to a non-private JMock5Mockery or JUnit5 won't use it
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();
}
}
- This allows dependents to use other versions of junit or other test frameworks (e.g. junit 5)
- Uses real method calls, not strings, so you can refactor more easily and autocomplete in the IDE.
- Customisation by delegation, not by inheritance.
- Many more plugin-points for customisation.
- Independent of any testing framework: compatability with the testing framework is a plugin-point.
- Can mock concrete classes without calling their constructors (if you really want to).
- Uses Hamcrest matchers, so can use a large and ever-growing library of matchers in expectations.
- Expectations match in first-in, first-out order, so tests are easier to understand.
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.
DSL-style API
Convenient classes that implement the APIs in the core, are used by the DSL-style API, and can be used in user-defined APIs
Classes integrating jMock with different testing APIs, such as JUnit 3.x, JUnit 4.x and TestNG.
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 implementation details
Tests for jMock itself
Controls the matching of invocations to expectations
Performs an action in response to an invocation
Wraps mock objects in an adapter of the correct type
Matches an invocation and fakes its behaviour
Translates expectation errors into error type used by a specific testing framework.
Creates names for mock objects based on the mocked type.
If you'd like to contribute, then do the following:
- clone this repository (
git clone …
) - install Maven (
brew install mvn
on Mac OS, for example) $ mvn package
in order to generate a signed JAR. This will run all the tests. ($ mvn test
appears not to suffice.)