Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Intro to SE ( ISAD 1 0 0 0 / 5 0 0 4 ) Worksheet 8 : White - Box Testing and Test Fixtures

Intro to SE (ISAD1000/5004) Worksheet 8: White-Box Testing and Test Fixtures ( Java)
CRICOS Provide Code: 00301J Page 1 of 5
public static void printCoordinates(double x, double y, double z)
{
System.out.printf("(%.2f,%.2f,%.2f)
", x, y, z);
}
Worksheet 10 : White-Box Testing and Test Fixtures
( Java)
Updated: 27th May, 2021,08th October 2023
There are two versions of this worksheet. This is the Java version.
In this worksheet, youll practice white-box test design, and the setting-up and tearing-down
of test fixtures. You may refer lecture 9 and 10 both when doing this worksheet There are a
range of production code methods for you to implement test code for. The code is shown
below, and is also available in Utils.java.
For each production code method, do the following:
(a) Design your test cases:
Identify the paths through the production code.
Select test data for each test case. In other words, for each path, select inputs (parameters, console input, and/or input files) that will cause the production code to
follow that path.
For each test case, determine the expected results. This includes return values,
exceptions thrown, console output, and output files.
(b) Implement yourtest cases.
Its good experience to continue to use JUnit, although you can still perform the exercise
without it.
(c) Run yourtests against the production code.
To ensure that your test code is working, its helpful to temporarily break the production
code. You could do this by editing the production code to alter the output/results very
slightly.
1. printCoordinates()
printCoordinates() takes in x, y and z coordinates, and prints them out in the format(x, y, z),
with two decimal places each. The output will end in a new line.
(This is a trivial case for test design. Since there are no conditional statements, there is only
one path, and hence one test case.)
Intro to SE (ISAD1000/5004) Worksheet 8: White-Box Testing and Test Fixtures ( Java)
CRICOS Provide Code: 00301J Page 2 of 5
Warning: Be very careful with code that deletes files! Ensure the file you specify is
definitely the one you want to delete.
public static char readChar(String validChars)
{
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
while(line.length()!=1||!validChars.contains(line))
{
line = sc.nextLine();
}
return line.charAt(0);
}
Note: Tearing-down console inputredirection in Java is much the same as for output:
import java.io.*;
...
InputStream originalIn = System.in; // Save original input
System.setIn(...); // Redirect input
...// Do testing
System.setIn(originalIn); // Restore input
2. readChar()
readChar() reads a single valid character from the user. The user enters a character, but
must re-enter their input if its invalid; i.e., if the character they enter does not occur within the
validChars parameter.
Hint: there are two paths, and hence two test cases here.
Note: Rememberto tear down everything that you set up. Specifically, to restore console output in Java, you must save a copy of the original, like this:
To delete temporary files:
import java.io.*;
...
new File("somefile.txt").delete();
import java.io.*;
...
PrintStream originalOut = System.out; // Save original output
System.setOut(...); // Redirect output
...// Do testing
System.setOut(originalOut); // Restore output
Intro to SE (ISAD1000/5004) Worksheet 8: White-Box Testing and Test Fixtures ( Java)
CRICOS Provide Code: 00301J Page 3 of 5
public static void guessingGame(int number)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer: ");
int guess = sc.nextInt();
while(guess != number)
{
if(guess > number)
{
System.out.println("Too high.");
}
else
{
System.out.println("Too low.");
}
System.out.print("Enter an integer: ");
guess = sc.nextInt();
}
System.out.println("Correct!");
}
public static double sumFile(String filename)
double sum =0.0;
try
Scanner sc = new Scanner(new File(filename)); // Throws IOException
while(sc.hasNextDouble())
sum += sc.nextDouble();
3. guessingGame()
guessingGame() runs a console-based number guessing game. The user is repeatedly asked to
guess what the number is, and is told whether their guess is too high, too low, or correct (at
which point the game ends).
Hint: the result of guessingGame() consists of all the console output, including the prompt asking for input. Also note that println() will print a newline character, while print() wont.
This will be important when determining the expected output.
4. sumFile()
sumFile(): opens a file (assumed to contain a list of numbers), and adds up the numbers, and
returns the total. If the file could not be opened, it returns -1 instead.
Hint: the empty string "" is an invalid filename that, by definition, cannot be opened.
Intro to SE (ISAD1000/5004) Worksheet 8: White-Box Testing and Test Fixtures ( Java)
CRICOS Provide Code: 00301J Page 4 of 5
// If using

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions