I am tasked with writing a small program along with a JUnit and Coverage test and I am having some issues with understanding how a JUnit will be written.
I have written JUnit tests before, with some help from more knowledgeable teammates, for other class projects. They have all been for classes that we could instantiate, use, and then inspect (with assert...()
).
The program we were told to write is a simple console application that takes in some values, computes something, and returns. Below is what I have written, it simply takes in a variable number of numbers between some range and computes the min, max, and average of the values.
The issue I am having is that I am not sure how I can write a JUnit for this in terms of how I have written them before. I am not sure how I can "falsify" user input to the console in the unit test.
I am asking for help in how I might go about writing a JUnit test for this piece of software.
package softqa_assignment2;
import java.io.Console;
import java.util.LinkedList;
public class SoftQA_Assignment2
{
public static void main(String[] args)
{
Console cons = System.console();
if (cons == null)
{
System.err.println("System could not provide console, exiting");
System.exit(1);
}
System.out.println("--------------------");
System.out.println("Welcome, this program computes the minimum, maximum, and average of 10-20 numbers.");
System.out.println("Input \"Q\" at anytime to quit.");
System.out.println("--------------------");
int numVals = 0;
boolean gotNumVals = false;
while(!gotNumVals)
{
String numValsStr = cons.readLine("Enter number of numbers (10-20): ");
if(numValsStr.equals("Q"))
{
System.err.println("Encountered \"Q\", quiting program...");
System.exit(1);
}
try
{
numVals = Integer.parseInt(numValsStr);
}
catch(NumberFormatException e)
{
System.out.println("Please input a integer value between 10 and 20.");
continue;
}
if(numVals < 10 || numVals > 20)
{
System.out.println("Please input a integer value between 10 and 20.");
continue;
}
else
gotNumVals = true;
}
System.out.println("--------------------");
int valsGot = 0;
LinkedList<Double> vals = new LinkedList<>();
while(valsGot < numVals)
{
String numValueStr = cons.readLine((valsGot + 1) + ": Enter a number: ");
if(numValueStr.equals("Q"))
{
System.err.println("Encountered \"Q\", quiting program...");
System.exit(1);
}
double numToAdd = 0;
try
{
numToAdd = Double.parseDouble(numValueStr);
}
catch(NumberFormatException e)
{
System.out.println("Please input a proper value.");
continue;
}
vals.add(numToAdd);
valsGot++;
}
System.out.println("--------------------");
System.out.println("Done getting numbers, computing output...");
System.out.println("--------------------");
double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
double avg = 0.0;
for(int i = 0; i < vals.size(); i++)
{
avg += vals.get(i);
if(vals.get(i) < min)
min = vals.get(i);
if(vals.get(i) > max)
max = vals.get(i);
}
avg /= vals.size();
System.out.println("Minimum: " + min + "\n Maximum: " + max + "\n Average: " + avg);
System.out.println("--------------------");
}
}
Aucun commentaire:
Enregistrer un commentaire