We’ll now show how to deal with objects that are returned by our method under test.

This will initialise our object using a constructor as explained in Class instantiation with constructor. Please read this tutorial first.

Github

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

The method

Below is the same simple method we used in the Complex arguments tutorial.

You’ll notice that this method returns a User object. Let's set up a simple test that can evaluate this as an output.

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

Define “valid” equivalence class

  1. If you have already completed the Complex arguments tutorial, you may see the above already. If not, then refer to that tutorial and create the representative with John Brook and 45.

Define the Output equivalence class

Next, we will define the output.

  1. Click the + icon to add a new valid equivalence class to Expected Value.

  2. Press the + icon to add a representative to this class.

  3. … or press the hamburger icon

  4. … and add it like this

At this point, a new screen will appear automatically

  • Click the dropdown and the following popup appears.

  • Select the User(name, age, bio) constructor. We choose this because the method under test modified the bio property and we want to check that it’s doing its job. Using this constructor lets us instantiate an object with the values that match the valid input representative as well as the bio field.

  • You could also have chosen Add New Factory Method if you wanted to instantiate an object using code. For more information on this, refer to Class instantiation with factory method. It’s the same process.

  • You are now shown the above screen. Complete the 3 fields as shown. It’s important that the name and age values you enter match the valid Input equivalence class.

  • We enter No bio available in the bio field because the method under test should set it to this string, if empty. We know that bio will be empty because the valid input used the setDefaultBioString(name, age) constructor which does not set bio.

  • Press Save and you are returned to the main editor screen.

Test cases and generate code

All we need to do now is complete the test cases and generate the code.

  1. We can see the valid representative that we’ve just defined. Double click if you want to modify it.

  2. If the DevMate recommendation engine has not loaded test cases, press the Generate Test Cases button.

  3. Make sure the radio button is selected as shown here.

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

Run the test

  1. Near the top of the file, you should see your test class ComplexArgsTest.

  2. You can run the test now (shows IntelliJ but obviously IDE and language dependent).

  3. We can see our test passing.

Conclusion

We’ve now seen how to work with object instantiation and return values. As we’ve shown in this and previous tutorials, you can always choose between factor methods and constructors, so you have full flexibility and control.