Over a million developers have joined DZone.

Why Do We Need to do Software Testing?

· Java Zone

Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience, brought to you in partnership with JetBrains

Why do we need to test our program?

When people talk about the importance of software testing, common examples they give frequently are military software, aircraft, etc. That is not concrete enough to understand why we need to test our software.

Here I provide an example from daily programming work. You don’t need to work for a military project to understand why we need software testing.

The example provided here is capitalize(String str) method from org.apache.commons.lang3.StringUtils which is from Apache Commons Lang library.

The following method capitalizes a given string. Suppose you write this method for your project. How do you know if this method is reliable and always return what it should return? We need to test! That is, given all different kinds of input, we see if it returns expected results. The returned results can either be good or bad, but they should be expected.

public static String capitalize(String str) {
    int strLen;
    if (str == null || (strLen = str.length()) == 0) {
        return str;
    }
    return new StringBuilder(strLen)
        .append(Character.toTitleCase(str.charAt(0)))
        .append(str.substring(1))
        .toString();
}

The testing code for this method is below. I have removed the code for testing other methods.

The testing code tests different kinds of inputs, such as null, an empty string, a single-char-string, and results of another method. In this way, we can assert this method is reliable.

@Test
public void testCaseFunctions() {
    assertEquals(null, StringUtils.capitalize(null));
 
    assertEquals("capitalize(empty-string) failed", "", StringUtils.capitalize("") );
 
    assertEquals("capitalize(single-char-string) failed", "X", StringUtils.capitalize("x") );
         
    // reflection type of tests: Sentences.
    assertEquals("capitalize(uncapitalize(String)) failed",
                     SENTENCE_CAP, StringUtils.capitalize(StringUtils.uncapitalize(SENTENCE_CAP)) );
 
    // reflection type of tests: One word.
    assertEquals("capitalize(uncapitalize(String)) failed",
                     FOO_CAP, StringUtils.capitalize(StringUtils.uncapitalize(FOO_CAP)) );
 
}

The assertEquals method is from JUnit framework which is the defacto standard of unit testing.

(source )



 

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

Topics:

Published at DZone with permission of Ryan Wang. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}