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.
You can find the code relating to this tutorial on Github in the
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
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
Define the Output equivalence class
Next, we will define the output.
Click the + icon to add a new valid equivalence class to Expected Value.
Press the + icon to add a representative to this class.
… or press the hamburger icon
… and add it like this
At this point, a new screen will appear automatically
Click the dropdown and the following popup appears.
User(name, age, bio)constructor. We choose this because the method under test modified the
bioproperty 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
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
agevalues you enter match the valid Input equivalence class.
No bio availablein the
biofield because the method under test should set it to this string, if empty. We know that
biowill be empty because the valid input used the
setDefaultBioString(name, age)constructor which does not set
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.
We can see the valid representative that we’ve just defined. Double click if you want to modify it.
If the DevMate recommendation engine has not loaded test cases, press the Generate Test Cases button.
Make sure the radio button is selected as shown here.
Press the Generate Test Code button and open the saved file.
Run the test
Near the top of the file, you should see your test class
You can run the test now (shows IntelliJ but obviously IDE and language dependent).
We can see our test passing.
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.