繁体   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