Description
JMock is a library that supports test-driven development of Java code with mock objects.
Mock objects help you design and test the interactions between the objects in your programs.
The jMock library:
makes it quick and easy to define mock objects, so you don't break the rhythm of programming.
lets you precisely specify the interactions between your objects, reducing the brittleness of your tests.
works well with the autocompletion and refactoring features of your IDE
plugs into your favourite test framework
is easy to extend.
JMock alternatives and similar libraries
Based on the "Testing" category.
Alternatively, view JMock alternatives based on common mentions on social networks and blogs.
-
Apache JMeter
Apache JMeter open-source load testing tool for analyzing and measuring the performance of a variety of services -
TestContainers
Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container. -
MockServer
MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding). -
PowerMock
PowerMock is a Java framework that allows you to unit test code normally regarded as untestable. -
Awaitility
Awaitility is a small Java DSL for synchronizing asynchronous operations -
AssertJ
AssertJ is a library providing easy to use rich typed assertions -
ArchUnit
A Java architecture test library, to specify and assert architecture rules in plain Java -
Pact JVM
JVM version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project. -
JSONAssert
Write JSON unit tests in less code. Great for testing REST interfaces. -
JMockit
Advanced Java library for integration testing, mocking, faking, and code coverage -
Citrus
Framework for automated integration tests with focus on messaging integration -
System Rules
A collection of JUnit rules for testing code which uses java.lang.System. -
junit-dataprovider
A TestNG like dataprovider runner for JUnit with many additional features -
ConcurrentUnit
Toolkit for testing multi-threaded and asynchronous applications -
Mutability Detector
Lightweight analysis tool for detecting mutability in Java classes -
Jukito
The combined power of JUnit, Guice and Mockito. Plus it sounds like a cool martial art. -
Lamdba Behave
A modern testing and behavioural specification framework for Java 8 -
Arquillian
Integration and functional testing platform for Java EE containers. -
Randomized Testing
Randomized Testing (Core JUnit Runner, ANT, Maven) -
Spectrum
A BDD-style test runner for Java 8. Inspired by Jasmine, RSpec, and Cucumber. -
Scott Test Reporter
Never debug a test again: Detailed failure reports and hassle free assertions for Java tests - Power Asserts for Java -
pojo-tester
Java testing framework for testing pojo methods. It tests equals, hashCode, toString, getters, setters, constructors and whatever you report in issues ;) -
raml-tester
Test if a request/response matches a given raml definition
Write Clean Java Code. Always.
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of JMock or a related project?
Popular Comparisons
README
JMock Library
Maven
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-junit5</artifactId>
<version>2.12.0</version>
<scope>test</scope>
</dependency>
Gradle
testCompile(
"junit:junit5:5.3.1",
"org.jmock:jmock-junit5:2.12.0"
)
Recent Changes
2.10.0
JUnit 5 Support
- 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();
}
}
JUnit 4 moved to provided scope in org.jmock:jmock
- This allows dependents to use other versions of junit or other test frameworks (e.g. junit 5)
Java7 Support will be dropped next release
2.9.0
- Dropped JDK 6 compliance.
- Exposed the InvocationDispatcher so that ThreadingPolicies
Upgrading to 2.8.X
Are you seeing NPEs?
We have had to make a breaking change to with()
. Tests using with(any(matcher))
for method signatures that require native types will throw NullPointerException
.
You should change
oneOf(mock).methodWithIntParams(with(any(Integer.class)));
to the following
oneOf(mock).methodWithIntParams(with.intIs(anything());
This is due to a compiler change in Java 1.7. The 2.6.0 release was compiled with Java 1.6 so it did not suffer this problem.
Advantages of jMock 2 over jMock 1
- 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.
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:
- 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.)