• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

Introducing: Mockagne (1 Viewer)


Dec 16, 2020
Reaction score
Mockagne is a fully dynamic mocking framework that is designed to be a Lua variant of the famous Java framework mockito. It was orignially written by Janne Sinivirta and is now a community project on Github.

In a Nutshell​

local mockagne = require("mockagne")
local mock = mockagne.getMock()
mockagne.when(mock.say(mockagne.any())).thenAnswer("Hello world")
-- ...
mock.ask("What's your name?")
mockagne.verify(mock.ask("What's your name?"))

What is it good for?​

Mocking is a form of behavioral software testing. It allows you to isolate the unit under test by replacing dependencies with so called "mocks". Mocks have the same interface as the dependency, but their behavior is configured in your tests rather than in the production code.

Let's assume you have a unit A which you want to test that depends on the units B and C. In a test for A you want to test A in isolation -- you are not interested in testing B and C at that time. This makes the tests more compact and test failures easier to locate.

In your unit test you create mocks for B and C. Then you configure their behavior for each test case.

Lastly if that is relevant in your test, you can check how the unit under test interacted with the interfaces of your mocks. This is for example useful if you want to check if A properly implements a protocol.

Where can I see a real-life example?​

The tests for the Lua module remotelog use Mockagne to mock a TCP socket on which the module depends. This safes us the trouble of setting up a TCP connection for that test.

When should I use Mocking?​

  1. Use mocking to isolate the unit under test.
  2. Prefer mocking your own code over foreign code.
  3. If you decide to mock foreign code, make sure you know how the external interface behaves and that it is stable!
  4. If you are tempted to mock dependency trees, consider a integration test instead of a unit test.
    As a rule of thumb: 5 mocks in the same test case are usually too many.

Where do I find more information?​

Check out the user-guide for more details about Mockagne.