This section again focuses on DevMate’s high level concepts, but using a Test Driven Development approach. Each subsequent tutorial goes into more detail and covers more scenarios.

Github

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


VIDEO - coming soon


Requirements

If you have not seen the previous tutorial, we have these requirements for a function.

It receives a positive integer as a string type, not as an integer. It should return the value as an integer type.

Invalid cases, which should throw an exception, are

- “10” (any other valid string representation of an integer)

Invalid cases are

- decimal string values such as “10.1”

- zero “0”

- any non-integer text strings such as “ten” or “xxx”

Creating the DevMate test

In TDD, we follow these principles.

  • Analyze the requirements (stated above)

  • Define your test cases. This, as you will see, is very intuitive with DevMate.

  • Run your test, which at this point should fail as we’ve not implemented any functional code.

  • Write some functional code

  • Re-run your tests

  • Refactor to address failing tests

  • Continue with the cycle of refactoring and running the test until the test passes.

This is actually a really simple process to follow and, using DevMate, is highly efficient compared to manually writing your tests.

Let’s see it all in action.

Our initial method

We have to set up a minimal function first with.

public static int parsePositiveIntString(String value) throws Exception {
    return 1;
}
CODE

Right click on the method name and then select Test with DevMate.

This will then create a .tmdl file. This will be saved to the src/test folder or the same folder as your class, depending on your project.

DevMate suggests the name of the parent class as the filename. For many cases, it makes sense to include all class methods to be tested in the same .tmdl file. We explain how to add more methods in the Scenarios and Chaining methods in one scenario tutorials.

DevMate Dialog

Next, you will see the DevMate dialog. We explained this in the Introduction to DevMate - Code First tutorial. We will show the detailed setup in the next tutorial Detailed Configuration - step by step.

Below is the preconfigured set of test cases. It takes only 2 or 3 minutes to configure once you’re familiar with DevMate.

Because our Product Owner or Domain Expert has provided the requirements in detail, we can define inputs to and outputs from the parsePositiveIntString method based purely on those requirements, before we’ve written a line of code.

We can just press the Generate Test Code button (we also support common C# test frameworks) and the test code is created.

Run tests

We can now run the test. Of course, it will fail as we haven’t implemented the method yet.

Refactor

We now start to implement our method properly. Here’s our first attempt.

public static int parsePositiveIntString(String value) {
    return Integer.parseInt(value);
}
CODE

Re-run tests

Well, that looks a lot better. But we’ve still got a failing test case - invalid:zero. This is because an exception is thrown if "0" is passed in.

Refactor again

Let’s fix it.

// Refactored code to fix the "0" test case
public static int parsePositiveIntString(String value) throws Exception {
    int convertedValue = Integer.parseInt(value);
    if(convertedValue == 0) throw new Exception("0 cannot be accepted");
    return convertedValue;
}
CODE

Re-run test

That’s more like it! We’re all done.

Conclusion

Once you’re familiar with DevMate, the above process would take only a couple of minutes - way quicker than writing the code by hand. Furthermore, it could even be done by technically oriented QA Engineers or Product Owners. In any case, it is fully readable by Domain Experts and Product Managers.

This approach actually frees up the developer to concentrate more heavily on writing functional code, which most developers will love.

As DevMate is clever, but avoids black magic, DevMate generated tests fit beautifully into any development and CI/CD pipeline without any disruption, producing efficient, human readable and maintainable tests.