簡體   English   中英

如何在我的 JUnit 測試中應用我的主文件中的方法?

[英]How to apply a method from my main file in my JUnit test?

我們的任務說我們應該“為一個名為sumArray的 function 編寫源代碼和測試代碼,它接受一個整數數組並返回數組中所有元素的總和”。

我想我有 SumArray.java 返回sum OK,但我正在努力將我的方法應用於測試輸入。 請問有什么幫助嗎? TIA。

SumArray.java

 package sumarray;
 
 import java.util.Scanner;
 
 public class SumArray {
 
     private static int n, sum = 0;
 
     public static int sumArray() {
         Scanner s = new Scanner(System.in);
         System.out.print("Enter no. of elements you want in array:");
         n = s.nextInt();
         int a[] = new int[n];
         System.out.println("Enter the elements. (Press [return] after each one)");
         for (int i = 0; i < n; i++) {
             a[i] = s.nextInt();
             sum = sum + a[i];
         }
         System.out.println("Sum:" + sum);
         return sum;
     }
 
     public static void main(String[] args) {
         sumArray();
     }
 }

SumArrayTest.java

 package sumarray;
 
 import org.junit.Test;
 import static org.junit.Assert.*;
 
 public class SumArrayTest {
 
     public SumArrayTest() {
     }
 
     /**
      * Test of main method, of class SumArray.
      */
     @Test
     public void testMain() {
         System.out.println("main");
         String[] args = null;
         SumArray.main(args);
         int[] intArray = new int[]{2, 3, 4};
         int expectedResult = 9;
 //        int testResult = sumArray({2, 3, 4});
         int testResult = SumArray sumArray(intArray);
         assertEquals(expectedResult, testResult);
 //        fail("The test case is a prototype.");
     }
 }

編輯:我已經嘗試通過一些更改來實施迄今為止的建議; 真的不確定這是否正確; 很多都是猜測TBH。

 package sumarray;
 
 import java.util.Scanner;
 
 public class SumArray {
 
     private static int n, sum = 0;
 
     public static int sumArray;
 
     public static int sumArray(int[] arr) {
         return sum;
     }
 
     public static SumArray input() {
         Scanner s = new Scanner(System.in);
         System.out.print("Enter no. of elements you want in array:");
         n = s.nextInt();
         int a[] = new int[n];
         System.out.println("Enter the elements. (Press [return] after each one)");
         for (int i = 0; i < n; i++) {
             a[i] = s.nextInt();
             sum = sum + a[i];
         }
         System.out.println("Sum:" + sum);
         return new SumArray();
     }
 
     public static void main(String[] args) {
         SumArray result = input();
         System.out.println(result.sumArray(SumArray));
     }
 }
 

 package sumarray;
 
 import org.junit.Test;
 import static org.junit.Assert.*;
 
 public class SumArrayTest {
 
     public SumArrayTest() {
     }
 
     @Test
     public void testSumArray() {
         System.out.println("main");
         String[] args = null;
         int[] intArray = new int[]{2, 3, 4};
         int expectedResult = 9;
         assertEquals(expectedResult, SumArray.sumArray(intArray)); 
 //        fail("The test case is a prototype.");
     }
 }

我目前看到的唯一錯誤是mainSumArray的“找不到符號”。

 package sumarray;
 
 import java.util.Scanner;
 
 public class SumArray {
 
     private static int n, sum = 0;
 
     public static int sumArray() {
         Scanner s = new Scanner(System.in);
         System.out.print("Enter no. of elements you want in array:");
         n = s.nextInt();
         int a[] = new int[n];
         System.out.println("Enter the elements. (Press [return] after each one)");
         for (int i = 0; i < n; i++) {
             a[i] = s.nextInt();
             sum = sum + a[i];
         }
         System.out.println("Sum:" + sum);
         return sum;
     }
 
     public static void main(String[] args) {
         sumArray();
     }
 }

以上是您發布的原始代碼。 現在,你說你得到了正確的 output。 是的,你可以這樣做:

 package sumarray;
 
 import java.util.Scanner;
 
 public class SumArray {
 
     private static int n, sum = 0;
 
     public static int sumArray() {
         Scanner s = new Scanner(System.in);
         System.out.print("Enter no. of elements you want in array:");
         n = s.nextInt();
         int a[] = new int[n];
         System.out.println("Enter the elements. (Press [return] after each one)");
         for (int i = 0; i < n; i++) {
             a[i] = s.nextInt();
             sum = sum + a[i];
         }
         System.out.println("Sum:" + sum);
         return sum;
     }
 
     public static void main(String[] args) {
         sumArray(); // this one will return the correct answer
         sumArray(); // this one will not
     }
   }

第二個會返回錯誤的數據,因為你沒有重置 sum 的值。

您應該拆分任務: sumArray 應該接收一個數組,並且應該返回元素的總和。 要么你應該改變方法的名字,要么改變實現,這就是 Mahmoud 告訴你的。

 package sumarray;
 
 import java.util.Scanner;
 
 public class SumArray {
     private static Scanner scan = new Scanner(System.in); // create this on class level, not every execution of your method
     
     public static int[] buildArray(int elements) {
        int[] arr = new int[elements];
        for ( int i = 0; i < elements; i++ ) {
            System.out.println("Enter element nr: " + (i+1));
            arr[i] = scan.nextInt();
        }
        return arr;
     }
     
     public static int sumArray(int[] input) {
        int sum = 0; // don't use a class level one. especially not a static one, it's value could be altered by another thread
        for ( int in : input ) { // iterate over the array and add the values
          sum += in; // this should be in -> each iteration we add the value of in (the element of the array) to sum
        }
        return sum;         
     }

 
     public static void main(String[] args) {
         System.out.println("Provide the size of the array: ");
         int param = scan.nextInt();
         int[] array = buildArray(param);
         int result = sumArray(array);
         System.out.println("The sum of the array is: " + result);
     }
   }

這種方法將使您遇到的問題要少得多。 它也沒有 static 變量,如 class 中的 n 和 sum 可能導致錯誤結果。

main() 方法是應用程序的入口點,您不應該測試 main() 方法。 相反,您應該測試sumArray()方法並比較預期的 Vs。 方法的實際返回值。

作為旁注,您可以更好地將輸入數組作為參數傳遞給sumArray()方法,而不是從方法主體內的System.in中讀取它。 因此,您的方法簽名可能如下所示:

public static int sumArray(int[] arr) 使用此方法的客戶端代碼(在您的案例(或單元測試)中是主要方法)可以傳遞數組,而無需打擾獲取此輸入數組的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM