ITEC 120
L05b: Testing Vowel Methods

Objectives

To successfully complete this lab you will write two methods and define and implement unit test cases for those methods.

Interactive programs vs. test drivers

For lab 4b, you wrote an interactive program to count vowels in a String. It prompted the user to enter a string, and reported to the user how many vowels were in the string they entered, and printed those vowels to the screen. Lab 4b was an interactive program: the user typed input, and your program printed output to the screen. For this lab, you will write a program that does the same thing, but in a different way. Instead of doing the work of counting vowels in the main method, you will create a service class (call it VowelSrv.java) with these two methods:

    int vowelCount(String s)
    String vowelString(String s)

The vowelCount method takes a String, s, as a parameter and returns an int, the number of vowels contained in String s. The vowelString method takes a String s as a parameter, and returns a String of all the vowels in s.

You will create a driver class (call it VowelDrv.java), containing the main method, where you will write a test driver which will call the methods in the server class. The purpose of this driver will be to test the methods you wrote to make sure they work correctly.

Testing

Software testing evaluates the quality of a product. When you buy a software application you expect the application to work as promised. Testing is the process that verifies that a piece of code does exactly what the code was designed to do. Thorough testing is critical for life support systems like the life support system on the space shuttle. D2L is a good example of an application or system that has not been tested well. You are expected to test every program before you submit your code.

Every good programmer excels at software testing. All great programmers are diligent about testing their code and they automate their tests so it is easy to test their code often. Assessment, a general term for evaluating quality, is a valuable skill for every professional in every industry. This course will emphasize testing as a necessary component of the software development process.

Testing is so important that our department offers a class: Software Testing (ITEC 335).

Testing may occur at different levels. A unit test evaluates a small unit of code such as a method. Integration testing evaluates how multiple units of code work together. System testing evaluates an entire system.

Code may be tested as a black box or a white box. Black box testing does not inspect the code during execution. The code is contained in a black box and only the inputs and outputs are visible to the tester. White box testing enables the tester to inspect the code during execution to determine exactly what the code is doing at each step. You will perform black box testing for this lab and for most of the semester.

Develop your test driver

Develop a test driver that tests the two vowel methods you wrote.

Your test plan

A test case is a single test. A test plan contains multiple test cases. A test driver impleĀ­ments specific test cases with known input and expected results. A test driver is not an interactive program. The test driver controls the input and verifies the output. Your test plan should be robust -- it should test that your code works for all possible types of inputs.

Usually, it is not possible to test every possible input. So, you try to test every possible type of input. You are trying to make sure that there is not some input that will "break" your code. That means different things depending on the task.

Consider the problem at hand: vowelCount and vowelString. They both take Strings as input. Three types of Strings come to mind: an empty String, a String of one character, and String of multiple characters. You should test those three cases.

Now think about the specifics of the problem: distinguishing vowels from other characters. You will want to make sure your code works for upper and lowercase vowels. How will we test the counting aspect of the method? Test Strings with no vowels, one vowel, and multiple vowels. Test Strings with multiple occurrances of the same vowel. With those things in mind, here's a list of test cases for vowelCount:

Can you think of any others? In a real world situation, try to think of every kind of input that could be sent to your method - even input the method was not necessarily designed to handle.

Write a list of test cases for vowelString.

A pass/fail test case

A test case passes when the actual result matches the expected result. A test case fails when the actual result does not match the expected result.

By printing the result of each test case (pass/fail) the test driver automates testing. The test plan may be executed by running the test driver. If any test fails the driver alerts the tester. The test driver may be scheduled to run every night and any test cases that fail may be written to a log file that is examined the next day. Automated testing makes it easy for developers to run tests, which improves quality. Running tests at night helps developers find and fix bugs on a daily basis.

Here's a single pass fail test case for vowelCount:

   // String of length 1 with one uppercase vowel
   System.out.print("Test number " + tstNum + ": ");
   if (srvObj.vowelCount("A") == 1) {
      System.out.println("Pass"); 
      } 
   else {
      System.out.println("Fail");
      }

In this example, tstNum is an int that simply contains the number of the test. Each test case can be numbered consecutively. The output of your test driver might look something like this:

   --- Testing the vowelCount method ---
   Test number 1: Pass
   Test number 2: Pass
   Test number 3: Pass
   Test number 4: Pass
   Test number 5: Pass
   Test number 6: Pass
   Test number 7: Pass
   Test number 8: Pass
   
   --- Testing the vowelString method ---
   Test number 1: Pass
   Test number 2: Pass
   Test number 3: Pass
   Test number 4: Pass
   Test number 5: Pass
   Test number 6: Pass
   Test number 7: Pass
   Test number 8: Pass

equals

The vowelCount method returns an int, and we compare it to the value we expect it to be using the operator ==.

The vowelString method returns a String. When we compare it's return value with what we expect it to be, if we use the == for that purpose, we will have a problem. Two Strings are == to each other if and only if they are the same string in memory. Thus, two different Strings, even if they contain the same characters, will not be == to each other. In our test cases, we want to know if two different Strings contain the same characters, in the same order. Use the String method .equals for this purpose.

Submit Your Assignment

Submit your VowelSrv.java and VowelDrv.java to the L05b dropbox on D2L.