Please refer to the previous tutorial Class instantiation with constructor. In this tutorial we will show how to use a factory method to instantiate an object.


You can find the code relating to this tutorial on Github in the /java/util/simplesteps folder.

Method under test

SimpleSteps is an extremely simple class. All it does is record the number of steps taken. It can be initialized with a number of steps.

The method under test in this tutorial is addSteps(). This does nothing other than add 1 to the number of steps taken.

Notice that we have implemented the equals() method for this class. This is important in any cases where you want to "assert equals" in the test. Please read this page for more information on implementing equals().


  • First, right-click the addSteps() method and choose Test with DevMate. Save the file. It will automatically open up the DevMate editor.

We should see the following screen. If you’ve run through this before, it will be pre-populated, in which case you can delete row configurations from the hamburger icon.

  1. The Instances section is at the top. Click on the + icon in the valid equivalence class. If you already had an entry, double click it.

You will see the following screen.

Click the dropdown and choose Add New Factory Method from the popup.

You will now see the following screen.

  1. Enter the name of your factory method.

  2. Click on the Add button to create a parameter to pass to the factory method.

  3. Select the type of the parameter. addSteps(int steps) clearly requires as int.

  4. Specify the name of the factory method argument.

  5. Press Save.

You will be taken back to the previous screen, where you’ll now see our newly configured parameter field int initialSteps.

  1. Enter the value you want to initialise SimpleSteps with, we’ll choose 100.

Press Save and you’ll be back in the main editor. Double-click to modify if you need to.

Generate test cases

Press the Generate Test Cases button. Before we can generate the test code, we need to define the output equivalence classes.

  1. Add a new valid representative. Because addSteps() returns void, DevMate will automatically create the Throws no exception representative for us.

  2. Then press Generate Test Cases.

Generating the code

  1. Click the radio button as shown so the test case is complete.

  2. Press the Generate Test Code button and open the saved file.

Editing our factory method

Unlike Class instantiation with constructor, you’ll need to write the implementation for your new factory method.

Near the top of the file you’ll see the boilerplate for the method that DevMate generated.

All you need to do now is write the implementation that initialises and returns a SimpleSteps object.

What next?

In this and the previous tutorial we’ve seen two different ways to initialise an object in DevMate.

Next, we’ll see how to deal with Complex return values. You may also want to look at Side effects and Exceptions to complete the picture.