An EasyMock inspired mocking library for erlang.
ErlyMock is the latest incarnation of an Erlang mocking library inspired by Easymock for Java. It is used in unit tests to verify that a set of functions is called by the code under test in correct order and with correct parameters.
With ErlyMock it is possible to declare the behavior of arbitrary modules/functions with or without expecting acutal invocations. The user simply declares what each function call to some function should return or do at the beginning of a unit test.
Assume there are two modules, used by the code to be tested, that shall be mocked (at least to prevent damaging side effects):
A rocket launcher server:
-module(rocket_launcher). launch(Longitude, Latitude, Type) -> ....
And A UI module:
-module(rocket_launcher_ui). ask_for_instructions() -> ...
Then this is how a happy case unit test might look like:
launche_missle_test() ->
% create a new mock process
M = em:new(),
% define the expectations
em:strict(M, rocket_launcher_ui, ask_for_instructions, [],
{return, [{longitude, 123},
{latitude, 999},
{type, some_rocket_type}]}),
em:strict(M, missle_lauchner, launch, [123, 999, some_rocket_type]),
% tell the mock that all expectations are defined
em:replay(M),
% run code under test
rocket_app:interactive(),
% verify expectations
em:verify(M).
Assume there is a new Project, that needs some unit testing with mocks. This is simple when using maven.
Assume there is already one source file called "borg.erl".
1. decide upon a short project name that would be a valid erlang atom without ' i.e. myproject
2. Create a directory by that name: mkdir myproject
3. Create a new maven project by writing a simple pom.xml which contains erlymock as dependency:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>your.group.id</groupId>
<artifactId>myproject</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>erlang-std</packaging>
<name>MyProject</name>
<description>Look ma' my project!</description>
<build>
<plugins>
<plugin>
<groupId>eu.lindenbaum</groupId>
<artifactId>maven-erlang-plugin</artifactId>
<version>2.0.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.github.sheyll</groupId>
<artifactId>em</artifactId>
<version>3.0.0</version>
<type>erlang-std</type>
<scope>test</scope>
</dependency>
</dependencies>
</project>
4. all necessary directories by running: "mvn erlang:setup"
5. copy sources to myproject/src/
6. copy unit tests into myproject/test_src/
Now you can run 'mvn test' to run eunit. Erlymock will automatically be downloaded by maven.
ErlyMock has undergone many stages and years of development and usage before reaching this stage. It was first published here: http://sheyll.blogspot.com/2009/02/erlang-mock-erlymock.html Then Samual Rivas cleaned it up and added support for restoring cover compiled modules for his project, see http://www.lambdastream.com/. The code was then added with all modifications to the great new erlang-maven-plugin forge which can be found here: http://sourceforge.net/projects/erlang-plugin/.
Then I decided to partially rewrite ErlyMock in order to provide a simpler API and in order to improve the code quality. Also, I wanted to use the gen_fsm OTP standard behavior.