如何解决如何在 JUnit 测试中应用主文件中的方法?
我们的任务是“为名为 sumArray
的函数编写源代码和测试代码,该函数接受一个整数数组并返回该数组中所有元素的总和”。
我想我已经让 SumArray.java 返回 sum
好的,但是我正在努力将我的方法应用于测试输入。请问有什么帮助吗? 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,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,4};
int expectedResult = 9;
assertEquals(expectedResult,SumArray.sumArray(intArray));
// fail("The test case is a prototype.");
}
}
我目前看到的唯一错误是 SumArray
中 main
的“找不到符号”。
解决方法
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();
}
}
以上是你贴出的原始代码。现在,你说你得到了正确的输出。是的,给你:
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);
}
}
这种方法会让您遇到的问题少得多。它也没有像 n 和 sum 这样的静态变量在你的类中可能会导致错误的结果。
, main() 方法是应用程序的入口点,您不应该测试 main() 方法。相反,您应该测试 sumArray()
方法并比较预期的 Vs。方法的实际返回值。
作为旁注,您可以更好地将输入数组作为参数传递给 sumArray()
方法,而不是从方法主体内的 System.in
读取它。
所以你的方法签名可以是这样的:
public static int sumArray(int[] arr)
。使用此方法的客户端代码(即您的案例(或单元测试)中的主要方法)可以传递数组,而无需担心如何获取此输入数组的方法。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。