DevMate can handle any exception or specific exception subclasses.

Github

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

Method under test

ExceptionDemo doesn’t do a lot.

We have two methods.

  • generalException() - will throw a general Exception if divisor = 0 .

  • userException() - will throw a DivideByZeroException that we've defined underneath if divisor = 0 .

Configuring DevMate

We are only going to show the userException() method being tested. The process the generalException() is very similar.

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

  1. Set a representative for the valid equivalence class for the number parameter.

  2. Set a representative for the valid equivalence class for the divisor parameter.

  3. Set a representative for the invalid equivalence class for the divisor parameter.

  4. Set a representative for the valid equivalence class for the output.

  5. Now we add our exception output. Click on the + icon and a valid equivalence class will be created.

  6. Click the hamburger icon next to valid and select Toggle Valid/Invalid so it changes to a red invalid. This is actually cosmetic but it’s worth doing for clarity’s sake.

You’re now ready to add the invalid representative.

  • Click on the + button next to the newly toggled invalid equivalence class.

  1. Click on the + icon

  2. Search the know classes by entering a few letters to help us quickly find DivideByZeroException.

  3. Click on it, once found and we’re done.

Generating Test Cases

  • Press Generate Test Cases. If there are test cases already present, you can delete them by pressing the Remove All Test Cases button in the top right.

  • You should now see the following screen. Complete the test cases definitions so you have what you see here.

  1. Valid case - expects value of 5 for 100 / 20.

  2. Invalid output case - expects an exception DivideByZeroException if divisor == 0.

  3. Press the Generate Test Code button.

Run the test

You can now open the test code file and run the test. It should pass.

Testing general/all exceptions

If you prefer, you can catch exceptions of any type by selecting Exception from the dropdown rather than a specific one.

Feel free to have a go!

Detectable exceptions

DevMate only works with exceptions that are explicitly stated in the method definition. Given

public static double userException(int number, int divisor) throws DivideByZeroException

it can pick up on DivideByZeroException thanks to throws DivideByZeroException. Without it, it would not appear in the dropdown list of available exceptions.