Java 是什么导致JUnit和系统规则在该学生身上失败';你的意见是什么?
在过去的一个月里,我一直在使用自动分级机,我的一个学生理解这些材料,但由于返回“null”,我的JUnit测试用例似乎总是不及格。由于这是一门Javai课程,他们目前仍在使用Java 是什么导致JUnit和系统规则在该学生身上失败';你的意见是什么?,java,junit,system-rules,Java,Junit,System Rules,在过去的一个月里,我一直在使用自动分级机,我的一个学生理解这些材料,但由于返回“null”,我的JUnit测试用例似乎总是不及格。由于这是一门Javai课程,他们目前仍在使用System.out.println()。正因为如此,我用它来解析它们的输出 学生意见书: import java.util.Scanner; public class DistanceFormula { public static void main(String[] args) { //Decla
System.out.println()
。正因为如此,我用它来解析它们的输出
学生意见书:
import java.util.Scanner;
public class DistanceFormula {
public static void main(String[] args) {
//Declarations
double distance;
double differenceX;
double differenceY;
double firstX;
double secondX;
double firstY;
double secondY;
//User Input for the First X value (X value for Point 1)
System.out.println("Enter In The value of X for point 1:");
Scanner inputFirstX = new Scanner(System.in);
firstX = inputFirstX.nextDouble();
//User Input for the First Y value (Y value for Point 1)
System.out.println("Enter In The value of Y for point 1:");
Scanner inputFirstY = new Scanner(System.in);
firstY = inputFirstY.nextDouble();
//User Input for Second X value (X value for point 2)
System.out.println("Enter In The value of X for point 2:");
Scanner inputSecondX = new Scanner(System.in);
secondX = inputSecondX.nextDouble();
//User Input for Second Y value (Y value for point 2)
System.out.println("Enter In The value of Y for point 2:");
Scanner inputSecondY = new Scanner(System.in);
secondY = inputSecondY.nextDouble();
//Find the difference of each point
differenceX = secondX - firstX;
differenceY = secondY - firstY;
//Distance Formula
distance = Math.sqrt((Math.pow(differenceX, 2)) + (Math.pow(differenceY, 2)));
//Displaying the outcome
System.out.println("Your First Point: " + firstX + "," + firstY + "\n" + "Your Second Point: " +
secondX + "," + secondY + "\n" + "The distance between the two points is: " + distance);
}
}
我的一个测试用例:
@Rule
public final StandardOutputStreamLog outlog = new StandardOutputStreamLog();
@Rule
public final TextFromStandardInputStream systemInMock = emptyStandardInputStream();
...
@Test(timeout=5000)
public void tests1() {
double x1 = 5.0;
double y1 = 5.0;
double x2 = 4.5;
double y2 = 4.5;
systemInMock.provideText(x1+"\n"+y1+"\n"+x2+"\n"+y2+"\n");
TestFile.main(new String[]{});
String reality = outlog.getLog().toLowerCase();
double calc = distance(x1,y1,x2,y2);
boolean result = reality.contains(calc+"");
assertTrue("Testing "+x1+" "+y1+" "+x2+" "+y2+" ", result);
}
最后,我的TestRunner:
// JUnit
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
// SimpleJSON
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
public class TestRunner {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
JSONObject obj = new JSONObject();
Result result = JUnitCore.runClasses(TestJunit.class);
double tests = result.getRunCount();
double fails = result.getFailureCount();
obj.put("tests", tests);
obj.put("fails", fails);
//System.out.println("Testing Complete\n---------------------------");
//System.out.printf("Run Time of Tests:\t%d ms\n", result.getRunTime());
//System.out.printf("Number of Tests:\t%d\n", (int)tests);
if (result.wasSuccessful()){
//System.out.println("All Tests Successful");
} else {
//System.out.println("Test Fails\n---------------------------");
JSONArray failures = new JSONArray();
System.out.printf("Number of Fails:\t%d\n", (int)fails);
for (Failure failure : result.getFailures()) {
String header = failure.getTestHeader();
String msg = failure.getMessage();
if (msg.contains("timed out")){
failures.add(header + " " + msg);
System.out.println(header + " " + msg);
} else {
failures.add(msg);
System.out.println(msg);
}
obj.put("failures", failures);
}
}
//System.out.println("Results\n---------------------------");
double grade = 100.0 * (double)((tests - fails)/tests);
obj.put("grade", grade);
System.out.printf("Grade:\t\t\t%.1f%%\n", grade);
System.out.print("JSON:\n");
System.out.println(obj);
}
}
在TestRunner中,if(msg.contains(“timed out”)
将出错,因为msg是null
如果我运行学生的代码或测试其他人的工作,测试运行得非常好。如果是我这边的问题,我想解决它。谢谢
扫描器
是缓冲的,因此每个构造的实例都消耗了模拟的系统中的文本。在
中,不仅仅是nextDouble
所需的文本。下一个Scanner
尝试读取同一底层流中剩余的内容,但输入已经被消耗,因此nextDouble
抛出NoTouchElementException
您可以尝试更准确地模拟
System.in
,方法是返回更少的字节(但不指示流的结束),直到检测到对System.out的下一次调用,或者更改方法并使用Runtime.exec
生成新的JVM并捕获该进程的所有输出。系统规则1.11.0中修复了该问题。您必须提供多行:
systemInMock.provideLines(
Double.toString(x1),
Double.toString(y1),
Double.toString(x2),
Double.toString(y2));
你能试着用failure.getException()来了解更多信息吗?我在其他测试用例运行时弄乱了他的代码,并将其更改为String str=failure.toString()
而不是getTestHeader()
和getMessage()
。我的输出是“”tests1(TestJunit):null"。我的想法是,这取决于他对多个扫描仪对象的实现,因为他对TestRunner的输出是输入点1的X值:输入点1的Y值:输入点1的X值:输入点1的Y值:
这肯定是由多个扫描仪造成的物体。修改学员代码以重复使用同一扫描仪会导致测试成功。@AdamG。我是系统规则的维护者。这是一个非常特殊的问题,但我试图在系统规则中解决它。