Over a million developers have joined DZone.

Why Do We Need to do Software Testing?

DZone's Guide to

Why Do We Need to do Software Testing?

· Java Zone
Free Resource

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

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)

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.

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 )


CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.


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

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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


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

{{ parent.tldr }}

{{ parent.urlSource.name }}