Custom Assertions let you write code to determine the outcome of the test case. You might want to write one if the standard approaches available in the Expected Value Output doesn’t fit the bill or if you simply want full control.

Github

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

Before we start

If you’ve run the previous tutorial in your IDE it should look like this.

If this is the case, reset everything it as follows.

  1. Click on the hamburger icon.

  2. Press Remove.

  3. Press Remove All Test Cases.

Adding a Custom Assertion - first way

  1. Click the + icon to add a new custom assertion.

  2. Click the + icon next to valid.

  1. Click in the dropdown and select Add New Custom Assertion Factory.

  • In the popup, enter the name of the factory method, for example IsDefaultBioSet then press Save.

  • Press Save again to return the main screen.

You will now see the following screen.

  1. You can see the newly added custom assertion factory successfully added.

  2. Press the Generate Test Cases button. This is a really simple use case so there will only be one. Afterwards, the test cases will appear as shown above.

  3. Click on this radio button, which will initially be empty.

  4. The Generate Test Code button will now be enabled. Press it, save the file and open it.

Editing the Custom Assertion Factory Method code

Below, you can see the generated code (In IntelliJ, but other IDEs / C# are the same in principle).

  1. Near the top of the file you can see the Custom Assertion. It will have boilerplate code but here we have replaced it with a basic assertEquals() statement.

  2. You can run the test.

  3. The test has run and passed.

Adding a Custom Assertion - second way

This is a variation on the first way, but it shows how you can pass values into your custom assertion factory method so you don’t have to hard code it.

In the same way we added a custom assertion to valid, add another one.

  1. Click the + icon to add a new custom assertion.

  2. Click the + icon next to valid.

  1. Click in the dropdown and select Add New Custom Assertion Factory, same as before.

We’ll now do something slightly different. We will tell DevMate to pass a parameter to our custom assertion factory method and specify it’s type.

  1. In the popup, enter the a different factory method name, for example IsDefaultBioSetWithParameter.

  2. Press the Add button to add a parameter.

  3. We need to specify the type of this parameter. You can search or open the dropdown list. In our case, we are going to specify the String type (java.lang.String).

  4. We specify the parameter name. As we are passing the expected value to the factory method, we’ll choose expected.

  5. Press Save.

You’ll then see the following screen with the freshly defined parameter available.

  1. Enter the “No bio available” string, which is set in the setDefaultBioString()method (see ComplexArgs.java).

The advantage of this approach is that if the default string is changed to something different in setDefaultBioString()then we can just change this field to match without having to find where it was hard-coded.

Change to the new assertion

  1. Make sure you first select our new custom assertion by clicking on the corresponding radio button.

  2. Generate the test code.

Editing the Custom Assertion Factory Method code

Below, you can see the generated code (shows IntelliJ, but other IDEs / C# follow the same principle).

  1. Near the top of the file you can see the first Custom Assertion we created.

  2. Here is the new one. It will have boilerplate code when you open it, but here we have already replaced the boilerplate.

  3. You will notice that the expected value parameter is no longer the string literal “No bio available”. When you enter data. in your IDE, the intellisense will show you the available properties. The correct property is hopefully clear. It is the name of the field you created earlier expected plus CustomMatcher, yielding expectedCustomMatcher.

Run test again and it should pass!

Conclusion

Now you understand the mechanics of custom assertions, you can hopefully see the ways and variations that will work for your real-world use cases.