Author: Haroon Khalil
-
Testing the indexOf method
The Apache Commons Lang has an interesting method called indexOf() with the following documentation, adapted from its Javadoc: Finds the index of the given value in the array starting at the given index. This method returns –1 for a null input array. A negative startIndex is treated as zero. A startIndex larger than the array length will return –1. Input…
-
Testing the unique method
The Apache Commons Lang offers the unique method. Following is its adapted Javadoc: Returns an array consisting of the unique values in data. The return array is sorted in descending order. Empty arrays are allowed, but null arrays result in a NullPointerException. Infinities are allowed. Parameters: The method returns a descending list of values included in the input array.…
-
The passing grade program
Consider the following requirement, inspired by a similar problem in Kaner et al.’s book (2013): A student passes an exam if they get a grade >= 5.0. Grades below that are a fail. Grades fall in the range [1.0, 10.0]. A simple implementation for this program is shown in the following listing. Listing 5.1 Implementation…
-
Introduction
So far, we have been doing example-based testing. We judiciously divide the input space of a program (into partitions), pick one concrete example from all the possible ones, and write the test case. What if we did not have to pick one concrete example out of many? What if we could express the property we are trying to exercise…
-
Tooling support
There is more and more support for pre- and post-condition checks, even in languages like Java. For instance, IntelliJ, a famous Java IDE, offers the @Nullable and @NotNull annotations. You can annotate your methods, attributes, or return values with them, and IntelliJ will alert you about possible violations. IntelliJ can even transform those annotations into proper assert checks at compile time. In addition,…
-
Should we write tests for pre-conditions, post-conditions, and invariants?
In a way, assertions, pre-conditions, post-conditions, and invariant checks test the production code from the inside. Do we also need to write (unit) tests for them? To answer this question, let me again discuss the difference between validation and pre-conditions. Validation is what you do to ensure that the data is valid. Pre-conditions explicitly state…
-
When not to use design-by-contract
Understanding when not to use a practice is as important as knowing when to use it. In this case, I may disappoint you, as I cannot see a single good reason not to use the design-by-contract ideas presented. The development of object-oriented systems is all about ensuring that objects can communicate and collaborate properly. Experience…
-
Exception or soft return values?
We saw that a possible way to simplify clients’ lives is to make your method return a “soft value” instead of throwing an exception. Go back to listing 4.5 for an example. My rule of thumb is the following:
-
Asserts and exceptions: When to use one or the other
Java does not offer a clear mechanism for expressing code contracts. Only a few popular programming languages do, such as F#. The assert keyword in Java is okay, but if you forget to enable it in the runtime, the contracts may not be checked in production. That is why many developers prefer to use (checked or unchecked)…
-
Input validation, contracts, or both?
Developers are aware of how important input validation is. A mistake in the validation may lead to security vulnerabilities. Therefore, developers often handle input validation whenever data comes from the end user. Consider a web application that stores products for an online store. To add a new product, a user must pass a name, a…