字符串数组总是返回最后一个元素,而不管索引号是多少



我正在使用JUnit对某个函数进行测试。如果输入错误,则设置错误消息。处理步骤所以,我试着测试信息是否正确。但是,由于某种原因,我将错误字符串数组与函数设置的错误数组进行比较,字符串数组似乎总是取最后一个元素,而没有正确地遍历数组元素。

这是I类测试With。

package ProgramFunctions;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.String;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.Vector;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
public class StudentGradeGenerator {
/*the error variable is used to store the error 
*raised by any function. which is used later to in the gui
*to display it to the user.
*/
private static String _error="";
/*the ErrorlineNumber is used to display the 
* line of the error.
* it's first initalized to zero, then in the 
* InputParse function the variable is increased for every
* line being parsed by one. 
*/
private static int ErrorlineNumber=0;
/*function to validate if the mark is:
* 1) of type int.
* 2) value between 0 and a maximum mark.
*/
public static int ValidateMark(String MarkName,String mark,int MaxMark)
{
try {
/*parse the string number in case of :
* success: this means that mark is of type int
* (then the value will be checked if it's between 0 & MaxMark).
* fail: an exception will occur. 
*/
int Mark =Integer.parseInt(mark);
/*check if the mark between 0 & MaxMark:
* if it's : the value is correct and will be returned.
* else: an error will be raised and the function will return with value -1.
*/
if(Mark>=0 && Mark<=MaxMark) return Mark;
_error ="Line ("+ErrorlineNumber+"): "+MarkName +" must be a value between 0 and "+ MaxMark +"!";
return -1;
}
catch(Exception e)
{
/*if we reached here then that's means the mark wasn't of type int.
* first raise an error then return with value -1.
*/
_error ="Line ("+ErrorlineNumber+"): "+MarkName+" must be of type int!";
return -1;
}
}
public static String getError(){
return _error;
}
}
下面是测试函数:
package ProjectUnitTest;
import static org.junit.jupiter.api.Assertions.*;
import java.net.URL;
import ProgramFunctions.StudentGradeGenerator;
import org.junit.jupiter.api.Test;

class StudentGradeGeneratorTest {
@Test
void TestFinalMark() {
int expectedIndex = 0;
String[] MarkInputs = new String[] { "d20", "-10", "0", "55", "60", "70" };
int[] exepected = new int[] { -1, -1, 0, 55, 60, -1 };

String[] FinalMarkExpectedErrorSet =new String[]{
"Line (0): Final Mark must be of type int!",
"Line (0): Final Mark must be a value between 0 and 60!",
"",
"",
"",
"Line (0): Final Mark must be a value between 0 and 60!"
};

for (var mark : MarkInputs) {
int actual = StudentGradeGenerator.ValidateMark("Final Mark", mark, 60);
assertEquals(exepected[expectedIndex], actual,String.format("mark::%s => Returning Error in validating Final Mark Function",mark));
assertEquals(StudentGradeGenerator.getError(),FinalMarkExpectedErrorSet[expectedIndex],String.format("mark::%s,index::%d =>Error in::Error set in Final Mark Function",mark,expectedIndex));
expectedIndex++;
}
}

这是我运行测试函数时出现的结果:

AssertionFailedError: mark::0,index::٢ =>Error in::Error set in Final Mark Function ==> expected: <Line (0): Final Mark must be a value between 0 and 60!> but was: <>
enter code here

虽然索引2在finalmarkexpectederorset是一个空字符串,但它似乎没有访问它?!

如果您想在每次函数执行之后测试_error变量,那么您还需要在函数开始之前重置该变量。

您在代码中遇到的问题是_error被声明为static,并且对于失败的测试,它实际上保留了先前执行函数时分配的值。

像下面这样稍微改变一下函数,现在应该可以工作了。

public static int ValidateMark(String MarkName,String mark,int MaxMark)
{
try {

// reset the error variable before execution
_error = "";
/*parse the string number in case of :
* success: this means that mark is of type int
* (then the value will be checked if it's between 0 & MaxMark).
* fail: an exception will occur. 
*/
int Mark =Integer.parseInt(mark);
/*check if the mark between 0 & MaxMark:
* if it's : the value is correct and will be returned.
* else: an error will be raised and the function will return with value -1.
*/
if(Mark>=0 && Mark<=MaxMark) return Mark;
_error ="Line ("+ErrorlineNumber+"): "+MarkName +" must be a value between 0 and "+ MaxMark +"!";
return -1;
}
catch(Exception e)
{
/*if we reached here then that's means the mark wasn't of type int.
* first raise an error then return with value -1.
*/
_error ="Line ("+ErrorlineNumber+"): "+MarkName+" must be of type int!";
return -1;
}
}

您的assertEquals()方法参数的顺序也不对。这就是为什么您也会看到不正确的错误消息。它的参数应该如下所示:

assertEquals(expectedValue, actualValue, message)