You will often want to instantiate an object before running a test.

In this tutorial we will cover how to do so from DevMate by using a constructor. You can also use a factory method, which we’ll cover in the Class instantiation with factory method tutorial.

Github

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().

Instantiation

  • First, right-click the addSteps() method an choose Test with DevMate and 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 SimpleSteps from the popup.

You will then be able to specify how you want to initialise the object. We’ve specified 100.

Click Save and you’ll be back in the main editor.

What next?

That’s all there is to initializing the instance using a constructor.

If you want more control, you can initialise the instance using a factory method.