分叉的Java VM异常退出。 JUnit测试?

时间:2013-03-18 03:01:19

标签: java junit

我有一个简单的Java程序似乎运行良好,直到上传到我学校的评分系统“WebCat”,我假设它正在运行JUnit。它踢回的错误是:

  

Forked Java VM异常退出。请注意,报告中的时间不会反映VM退出前的>时间。

我已经研究过这个问题,主要的第一个故障排除步骤似乎是查看转储日志。不幸的是,在这种情况下我无法做到这一点。考虑到评分系统缺乏反馈以及缺乏编译或运行时错误,我真的对如何开始排除故障感到茫然。

以下是代码,如果有人熟悉此错误或至少可以给我一些指导,从哪里开始故障排除。非常感谢!

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.io.IOException;



class PlayerApp {
    public static void showMenu()
    {
        System.out.println("Player App Menu");
        System.out.println("P - Print Report");
        System.out.println("A - Add Score");
        System.out.println("D - Delete Score");
        System.out.println("L - Find Lowest Score");
        System.out.println("H - Find Highest Score");
        System.out.println("Q - Quit");
    }
    public static void main(String[] args) throws IOException 
    {   
        if (args.length == 0)
        {
            System.out.println("File name was expected as a run argument.");
            System.out.println("Program ending.");
            System.exit(0);
        }
        String fileName = args[0];
        Scanner sc = new Scanner(System.in);
        String stnew = "";
        boolean exit = false;
        Player p = null;
        double[] scoreList;

        File dbFile = new File(fileName);
        FileInputStream fis = new FileInputStream(fileName); 
        InputStreamReader inStream = new InputStreamReader(fis); 
        BufferedReader stdin = new BufferedReader(inStream);
       String name = stdin.readLine();
       stnew = stdin.readLine();
       int numScore = Integer.parseInt(stnew);
       scoreList = new double[numScore];
       for (int i = 0; i < numScore; i++)
       {
          stnew = stdin.readLine();
          scoreList[i] = Double.parseDouble(stnew);
       }

       p = new Player(name, numScore, scoreList);

       stdin.close();

        System.out.println("File read in and Player object created.");
       showMenu();
       while (exit == false)
       {

        System.out.print("\nEnter Code [P, A, D, L, H, or Q]:");
        String choice = sc.nextLine().toLowerCase();
        if (choice.equals("p"))
        {
            System.out.println(p.toString());
        }
        else if (choice.equals("a"))
        {
            System.out.print("   Score to add: ");
            stnew = sc.nextLine();
            double scoreIn = Double.parseDouble(stnew);
            p.addScore(scoreIn);
        }
        else if (choice.equals("d"))
        {
            System.out.print("   Score to delete: ");
            stnew = sc.nextLine();
            double scoreIn = Double.parseDouble(stnew);
            p.deleteScore(scoreIn);
            System.out.println("   Score removed.");
        }
        else if (choice.equals("l"))
        {
            System.out.println("   Lowest score: " + p.findLowestScore());
        }
        else if (choice.equals("h"))
        {
            System.out.println("   Highest score: " + p.findHighestScore());
        }
        else if (choice.equals("q"))
        {
            exit = true;
        }
        }


   }
}

断裂

import java.text.DecimalFormat;



public class Player {

    //Variables
    private String name;
    private int numOfScores;
    private double[] scores = new double[numOfScores];

    //Constructor
    public Player(String nameIn, int numOfScoresIn, double[] scoresIn) {
       name = nameIn;
       numOfScores = numOfScoresIn;
       scores = scoresIn;
   }

    //Methods
    public String getName() {
       return name;
    }
    public double[] getScores() {
       return scores;
    }
    public int getNumScores() {
       return numOfScores;
    }
    public String toString() {

        String res = "";
        DecimalFormat twoDForm = new DecimalFormat("#,###.0#");
      DecimalFormat twoEForm = new DecimalFormat("0.0");
        res += "   Player Name: " + name + "\n   Scores: ";
        for (int i = 0; i < numOfScores; i++)
        {
            res += twoDForm.format(scores[i]) + " ";
        }
        res += "\n   Average Score: ";
        res += twoEForm.format(this.computeAvgScore());
        return res;
    }
    public void addScore(double scoreIn) {
       double newScores[] = new double[numOfScores +1 ];
       for (int i = 0; i < numOfScores; i++)
       {
           newScores[i] = scores[i];
       }
       scores = new double[numOfScores + 1];
       for(int i = 0; i < numOfScores; i++)
       {
           scores[i] = newScores[i];
       }
       scores[numOfScores] = scoreIn;
       numOfScores++;
    }
    public boolean deleteScore(double scoreIn) {
        boolean found = false; 
        int index = 0;
        for (int i = 0; i < numOfScores; i++)
        {
           if (scores[i] == scoreIn)
            {
                found = true;
                index = i;
            }
        }
        if (found == true)
        {
            double newScores[] = new double[numOfScores -1 ];
            for (int i = 0; i < index; i++)
            {
               newScores[i] = scores[i];
            }
            for (int i = index + 1; i < numOfScores; i++)
            {
                newScores[i - 1] = scores[i];
            }
            scores = new double[numOfScores - 1];
            numOfScores--;
            for (int i = 0; i < numOfScores; i++)
            {
                scores[i] = newScores[i];
            }
            return true;
        }
        else
        {
            return false;
        }


    }
    public void increaseScoresCapacity() 
    {
        scores = new double[numOfScores + 1];
        numOfScores++;
    }
    public double findLowestScore() {
        double res = 100.0;
        for (int i = 0; i < numOfScores; i++)
        {
            if (scores[i] < res)
            {
                res = scores[i];
            }
        }
        return res;
    }
    public double findHighestScore() {
        double res = 0.0;
        for (int i = 0; i < numOfScores; i++)
        {
            if (scores[i] > res)
            {
                res = scores[i];
            }
        }
        return res;
    }
    public double computeAvgScore() {
        double res = 0.0;
      if (numOfScores > 0) {
           for (int i = 0; i < numOfScores; i++)
        {
               res += scores[i];
           }
           return res / (double)(numOfScores);
      }
      else {
         //res = 0.0;
         return res;
      }
    }

}

2 个答案:

答案 0 :(得分:5)

您的程序正在调用System.exit(0)以获取某些输入。不要那样做!这正是消息告诉你的内容:在评分代码完成之前,JVM退出文本中间。而不是致电exit(),只需使用return提前从main()返回。

答案 1 :(得分:1)

System Rules有一个名为ExpectedSystemExit的JUnit规则。使用此规则,您可以测试调用System.exit(...)的代码。