In this tutorial we look at how you pass an object from an Equivalence Class to the method under test.
You can find the code relating to this tutorial on Github in the
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 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
setEmptyBioStringmethod 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.
Click the dropdown below the Choose Initializer field and and a popup will appear. Select the constructor shown below. We don’t want
bioto be initialized for our example as we want our method to be able to set the default text, which it will only do it
You should now see the screen below.
Fill in the fields as shown.
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.
Add a new Expected Value
Add a new value within the valid equivalence class by clicking the + icon. When you do this, a dialog will appear.
Click in the dropdown field, which will initially contain
null(). In the popup that appears, select the constructor User(name, age, bio).
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
biofield 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".
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.
Press the button to generate the test cases.
You should click on the expected value for this (the only) test case.
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!