Test Doubles - mocks, stubs etc.
We will assume familiarity with the concept of mocking. If you are new to testing then we recommend you familiarize yourself with them first.
Github
You can find the code relating to this tutorial on Github in the /java/mocking
folder.
Sample use case
Please clone the Github repo and open it in your IDE.
We have set up a simple REST API in the project.
In the /java/org.example
, the main
function
Initialises a simple web server.
Initialises a user service and a database repository method.
Initialises a Controller to handle REST API calls.
This project is runnable if you’re using Java. Apologies to C# developers. We will be supplying a C# version shortly.
In the /java/mocking
folder you can find the controller, services and database repository source code.
In this example, we are going to test the User Service method simpleAddUser()
. Here's the whole UserService
class.

You can see that
simpleAddUser()
is dependent on the external method addUserRepository.execute().
In order to create a unit test, we will need to use a test double in order test simpleAddUser()
in isolation.
DevMate configuration
Right click on simpleAddUser()
and select Test with DevMate and save.
Let’s go ahead and configure the Inputs and Outputs you see below to start with.

We’ll deal with the Instance creation in a moment.
Valid representative for the
name
parameter.Invalid representative (name requires least 3 letters) for the
name
parameterValid representative for the
age
parameter.Valid representative for the valid Expected output. Please refer to Complex return value if you’ve forgotten how to do this. You should expect back a
User
object containing "John Brook"
,34
and“No bio available"
.Add an invalid representative of type
Exception
.
Instantiating UserService
We need to instantiate a UserService object. If you’ve been good and followed the previous tutorials, this process should be familiar. However, it’s a little bit more advanced because we have to
create an instance of the UserService, which requires a UserRepository instance as a parameter
create the factory method that will handle the mocking logic.
Next
Create a new valid representative for the Valid equivalence class of the UserService (see Point 1. in the above screenshot).
The screen that appears with have an initializer of null at this point.
Click on the initializer dropdown and choose the UserService constructor shown below.

It should then look this this.

This should now the populated with the constructor you just selected.
Click the edit icon so we can specify how to create a test double/mock for
addUserRepository.execute()
.
You will now see another initializer screen. This time, we want to initialise our test double/mock with a factory method.
Click in the initializer drop down and select Add New Factory Method.


Provide the name of your factory method.
Configure any parameters you might want to pass to your factory method.
Press save and you can now provide some parameter values to pass to the factory method.

Using parameters like this means that we don’t have to hard code any values in our factory method and can take advantage of the DevMate UI to change values if required.
Press Save and you’ll be returned to the main DevMate editor screen.
Generate test cases
Now that we’re all done with the configuration, we can generate test cases.
Press the Generate Test Cases button and complete the test cases definition so it looks like this.

Press Generate Test Code button and open the test file.
Implementing the factory method
You should find the following code snippet in the test file.

Let’s replace the body with a quick implementation that creates the mock.

Run the test
You can now run the test and it should pass both test cases.