dimanche 2 août 2020

Junit test for scanner parameter filereader

What I am trying to do is to simply write a test for the calcGrade(scanner sc) function, but I don't know if it's possible to test it in this scenario since the scanner reads in grades in a textfile then returns an average of them.

public static void main(String[] args) throws FileNotFoundException {
        
        Scanner sc = new Scanner(new FileReader("./Src/Chapter3/grade.txt"));
        
        System.out.printf("The average grade is: %.2f", calcGrade(sc));
                
    }

    public static double calcGrade(Scanner sc) {
        double test1, test2, test3, test4, test5, avg;

        test1 = sc.nextDouble();
        test2 = sc.nextDouble();
        test3 = sc.nextDouble();
        test4 = sc.nextDouble();
        test5 = sc.nextDouble();
        avg = (test1 + test2 + test3 + test4 + test5) / 5.0;
        
        sc.close();
        return avg;
    }

 @Test

    public void testCalGrade() {
    double expected = 70.0;
    double actual = js.calcGrade(); //i could change the parameter here instead passed in scanner to the individual grades then test it
    assertEquals(expected, actual,0.02);
}

grade.txt

80 98 100 78 65.6

Aucun commentaire:

Enregistrer un commentaire