0

I'm new to test harness writing so I'd like some tips on how to get started. I have some code for finding the smallest element in an array and I'm not sure what I need to change to the method so I can begin writing my test harness around the method. I know that I shouldn't have the method in a main class, but I'm not sure how to define the same method so it's not in the main class.

I just need to know what would be the first thing I would do to start writing my test harness for this program.

import java.util.Scanner;

public class Fross_Charles_PA4
{
    public static void main(String args[])
   {
       int small, size, i;
       int arr[] = new int[50];
       Scanner scan = new Scanner(System.in);
       
       System.out.print("Enter Array Size : ");
       size = scan.nextInt();
       
       System.out.print("Enter Array Elements : ");
       for(i=0; i<size; i++)
       {
           arr[i] = scan.nextInt();
       }
       
       System.out.print("Searching for the Smallest Element....\n\n");
       
       small = arr[0];
       
       for(i=0; i<size; i++)
       {
           if(small > arr[i])
           {
               small = arr[i];
           }
           
       }
       
       System.out.print("Smallest Element = " + small); 
   }
}
DjMaxLETU
  • 41
  • 1
  • 7
  • Well, the first step is to give it a name other than `main`, and call it from `main`. It finds the smallest element in an array, so it should take an array as input parameter and return the smallest element. How about `public static int findSmallestElement(int[] input)`? Then you can write unit tests for that, to see what it returns for various inputs. Providing console input to a test is trickier, so you better separate that from the calculation. – Hulk Sep 22 '20 at 04:51
  • That said, this topic is too broad for this site - and you did not actually ask a question. Experiment a bit, and ask a more specific question once you encounter a problem. – Hulk Sep 22 '20 at 05:02

0 Answers0