In this tutorial we look at how you pass an object from an Equivalence Class to the method under test.

Please also refer to Custom Assertions and Side Effects to understand two further ways to handle methods that return an object or modify a class instance.

Github

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

Before we start

If you already have a ComplexArgsTest.tmdl file in this folder, you might want to delete it if you want to follow all the steps in your IDE.

The method

The method we will use is very simple indeed.

It expects a User object. If the bio property is an empty string, then it just sets it to the default text "No bio available" and returns the modified object.

Open the DevMate editor

  • Right click on the setEmptyBioString method and choose Test with DevMate and press Save.

  • You should now see the following

Configuring the “Valid” equivalence class

  • Click on the + icon next to valid, labelled 1. in the above screenshot.

  • A dialog should now appear. If it doesn’t, or you’ve done step previously, double click the value (null) that is beneath valid.

  1. Click the dropdown below the Choose Initializer field and and a popup will appear. Select the constructor shown below. We don’t want bio to be initialized for our example as we want our method to be able to set the default text, which it will only do it bio is empty.

  • You should now see the screen below.

  • Fill in the fields as shown.

  • Press Save.

A few words about “Add Factory Method”

In the above popup image, you can see Add New Factory Method. We won’t be using this now but it’s worth mentioning.

This option allows you to define a factory method that can be used to instantiate your object. In some cases you will want the flexibility that hands-on control gives you. You can see examples of Factory Methods being used in the Class instantiation with a factory method and Custom Assertions tutorials.

You will see the factory method in the generated test code and can then write whatever code you require to handle the instantiation.

Configuring the expected outputs

We won’t worry about any more equivalence classes. Let’s jump to the outputs. We’re going to keep this very basic, too. The following tutorials explain this in detail.

Because we have to define at least some output, we’ll tell DevMate to use one of the constructors to create an instance to test against.

  1. Add a new Expected Value

  2. Add a new value within the valid equivalence class by clicking the + icon. When you do this, a dialog will appear.

  1. Click in the dropdown field, which will initially contain null(). In the popup that appears, select the constructor User(name, age, bio).

  2. Fill in all three fields. As we are comparing against an input equivalence class value we will be expecting the same field values we used for the input (John Brook, 34). In the bio field we need to supply the expected value. If refer back to the method's code, we know it should set it to "No bio available".

  • Press Save.

Generate test cases

We can now generate our test cases. We will only expect one test case as this is such a simple use case.

  1. Press the button to generate the test cases.

  2. You should click on the expected value for this (the only) test case.

  3. As soon as you have done the previous step, you’ll see that the Generate Test Code button is enabled.

Generate the test and run it

Press the Generate Test Code button, save the test file and open it.

Your IDE (we’re showing IntelliJ in this screenshot) will let you run the test. It should pass if you’ve followed all the steps!

Implementing the equals method

You don’t need to worry about this observation here, but it is usually very important that you implement an equals() method in your class.