Discuss / Java / 求教,这我要怎么才能避免在测试中避免遇到用户输入啊?

求教,这我要怎么才能避免在测试中避免遇到用户输入啊?

下面是我的Fileoperator class。那个mainhelp method我会在Main class中用。

public class Fileoperator {    private ArrayList<Person> saveList;    private Scanner scanner;    int age;    String name;    int income;    int expenditure;    int willingnesstoInvest;    String exit;    public Fileoperator() {        saveList = new ArrayList<>();        scanner = new Scanner(System.in);        processOperations();    }    public void saveListSetter(ArrayList<Person> input) {        saveList = input;    }    public ArrayList<Person> saveListGetter() {        return saveList;    }    private void processOperations() {        while (true) {            Person personEntry = setPerson();            if (exit.equals("yes")) {                personEntry.updateMaster(age, name, income, expenditure, willingnesstoInvest);                personResult(personEntry);                break;            }            personEntry.updateMaster(age, name, income, expenditure, willingnesstoInvest);            personResult(personEntry);            System.out.println(personEntry.print());        }        for (Person p: saveList) {            System.out.println(p.print());        }    }    private Person setPerson() {        Person personEntry = new Person(0, "", 0, 0, 0);        System.out.println("Age: ");        age = scanner.nextInt();        scanner.nextLine();        System.out.println("Name: ");        name = scanner.nextLine();        System.out.println("Income: ");        income = scanner.nextInt();        System.out.println("Expenditure:");        expenditure = scanner.nextInt();        System.out.println("Willingness to invest: ");        willingnesstoInvest = scanner.nextInt();        scanner.nextLine();        System.out.println("done?(yes or no)");        exit = scanner.nextLine();        return personEntry;    }    private void personResult(Person p) {        saveList.add(p);    }    public static void mainhelper() {        try {            printload(load());        } catch (IOException e) {            e.printStackTrace();        }        Fileoperator fo = new Fileoperator();        try {            fo.save();        } catch (IOException e) {            e.printStackTrace();        }    }    public void save() throws IOException {        List<String> lines = Files.readAllLines(Paths.get("output.txt"));;        PrintWriter writer = new PrintWriter("output.txt","UTF-8");        for (Person p: saveList) {            lines.add(p.getAge() + ", " + p.getName() + ", "                    + p.getIncome() + ", " + p.getExpenditure() + ", " + p.getwillingnesstoInvest());        }        for (String line : lines) {            ArrayList<String> partsOfLine = splitOnSpace(line);            System.out.println("Age: " + partsOfLine.get(0) + ", ");            System.out.println("Name: " + partsOfLine.get(1) + ", ");            System.out.println("Income " + partsOfLine.get(2) + ", ");            System.out.println("Expenditure " + partsOfLine.get(3) + ", ");            System.out.println("WillingnesstoInvest " + partsOfLine.get(4) + ", ");            writer.println(line);        }        writer.close(); //note -- if you miss this, the file will not be written at all.    }    public static ArrayList<String> splitOnSpace(String line) {        String[] splits = line.split(", ");        return new ArrayList<>(Arrays.asList(splits));    }    public static ArrayList<Person> load() throws IOException {        List<String> lines = Files.readAllLines(Paths.get("output.txt"));        ArrayList<Person> loadList = new ArrayList();        for (String line : lines) {            if (line.equals("")) {                break;            } else {                Person fromline = fromline(line);                loadList.add(fromline);            }        }        return loadList;    }    public static void printload(ArrayList<Person> loadList) {        for (Person p: loadList) {            System.out.println(p.print());        }    }    private static Person fromline(String line) {        Person fromline = new Person(0, "", 0, 0, 0);        ArrayList<String> partsOfLine = splitOnSpace(line);        int age = Integer.parseInt(partsOfLine.get(0));        int income = Integer.parseInt(partsOfLine.get(2));        int expenditure = Integer.parseInt(partsOfLine.get(3));        int willingnesstoInvest = Integer.parseInt(partsOfLine.get(4));        fromline.updateMaster(age, partsOfLine.get(1), income, expenditure, willingnesstoInvest);        return fromline;    }}

然后下图是我的Fileoperatortest class。

public class FileoperatorTest {    @Test    public void testload() throws IOException {        Person d = new Person(20,"David",0,20000,2);        Person r = new Person(30,"Random guy",50000,30000,2);        int index = 0;        ArrayList<Person> check = new ArrayList<> ();        check.add(d);        check.add(r);        load();        for (Person l: load()) {            assertEquals(l.getAge(), check.get(index).getAge());            assertEquals(l.getName(), check.get(index).getName());            assertEquals(l.getIncome(), check.get(index).getIncome());            assertEquals(l.getExpenditure(), check.get(index).getExpenditure());            assertEquals(l.getwillingnesstoInvest(), check.get(index).getwillingnesstoInvest());            index = index + 1;        }    }    @Test    public void testsave() throws IOException {        Person test = new Person(30,"test subject",50000,30000,2);        ArrayList<Person> check = new ArrayList<> ();        check.add(test);        Fileoperator fo = new Fileoperator();        fo.saveListSetter(check);        fo.save();        assertEquals(load().get(3).getAge(), check.get(1).getAge());        assertEquals(load().get(3).getName(), check.get(1).getName());        assertEquals(load().get(3).getIncome(), check.get(1).getIncome());        assertEquals(load().get(3).getExpenditure(), check.get(1).getExpenditure());        assertEquals(load().get(3).getAge(), check.get(1).getAge());    }}

我在这里遇到了一个问题,本来设计Fileoperator class的时候,我是想着输入user inputs然后用save method保存起来的。但是我没法直接用save method,只能新建一个Fileoperator fo然后才能用fo.save()。然后test的时候save method的test就运行不了,我没法输入数据。

这里我觉得我的问题是就是我把processOperator method放到了Fileoperator的constructor里面然后在processOperator里面我又把setPerson method(需要用到user inputs的method)的值指定给了Person personEntry,PersonEntry会被personResult method拿来创造saveList(这个就是我要用到save method存到output.txt的ArrayList)。我不知道Fileoperator class有没有什么地方能改写的,能让我在保留setPerson method的同时允许我在test class里面能够test save method的。

国外留学刚开始学java,好多东西弄不懂。用了英文还请见谅。

乱了,怎么改。。。

重新发试试

public class Fileoperator {
    private ArrayList<Person> saveList;
    private Scanner scanner;
    int age;
    String name;
    int income;
    int expenditure;
    int willingnesstoInvest;
    String exit;

    public Fileoperator() {
        saveList = new ArrayList<>();
        scanner = new Scanner(System.in);
        processOperations();
    }

    public void saveListSetter(ArrayList<Person> input) {
        saveList = input;
    }

    public ArrayList<Person> saveListGetter() {
        return saveList;
    }

    private void processOperations() {
        while (true) {
            Person personEntry = setPerson();

            if (exit.equals("yes")) {
                personEntry.updateMaster(age, name, income, expenditure, willingnesstoInvest);
                personResult(personEntry);
                break;
            }
            personEntry.updateMaster(age, name, income, expenditure, willingnesstoInvest);
            personResult(personEntry);
            System.out.println(personEntry.print());
        }
        for (Person p: saveList) {
            System.out.println(p.print());
        }
    }

    private Person setPerson() {
        Person personEntry = new Person(0, "", 0, 0, 0);
        System.out.println("Age: ");
        age = scanner.nextInt();
        scanner.nextLine();
        System.out.println("Name: ");
        name = scanner.nextLine();
        System.out.println("Income: ");
        income = scanner.nextInt();
        System.out.println("Expenditure:");
        expenditure = scanner.nextInt();
        System.out.println("Willingness to invest: ");
        willingnesstoInvest = scanner.nextInt();
        scanner.nextLine();
        System.out.println("done?(yes or no)");
        exit = scanner.nextLine();
        return personEntry;
    }

    private void personResult(Person p) {
        saveList.add(p);
    }

    public static void mainhelper() {
        try {
            printload(load());
        } catch (IOException e) {
            e.printStackTrace();
        }
        Fileoperator fo = new Fileoperator();
        try {
            fo.save();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void save() throws IOException {
        List<String> lines = Files.readAllLines(Paths.get("output.txt"));;
        PrintWriter writer = new PrintWriter("output.txt","UTF-8");
        for (Person p: saveList) {
            lines.add(p.getAge() + ", " + p.getName() + ", "                    + p.getIncome() + ", " + p.getExpenditure() + ", " + p.getwillingnesstoInvest());
        }
        for (String line : lines) {
            ArrayList<String> partsOfLine = splitOnSpace(line);
            System.out.println("Age: " + partsOfLine.get(0) + ", ");
            System.out.println("Name: " + partsOfLine.get(1) + ", ");
            System.out.println("Income " + partsOfLine.get(2) + ", ");
            System.out.println("Expenditure " + partsOfLine.get(3) + ", ");
            System.out.println("WillingnesstoInvest " + partsOfLine.get(4) + ", ");
            writer.println(line);
        }
        writer.close(); //note -- if you miss this, the file will not be written at all.    }

    public static ArrayList<String> splitOnSpace(String line) {
        String[] splits = line.split(", ");
        return new ArrayList<>(Arrays.asList(splits));
    }

    public static ArrayList<Person> load() throws IOException {
        List<String> lines = Files.readAllLines(Paths.get("output.txt"));
        ArrayList<Person> loadList = new ArrayList();
        for (String line : lines) {
            if (line.equals("")) {
                break;
            } else {
                Person fromline = fromline(line);
                loadList.add(fromline);
            }
        }
        return loadList;
    }

    public static void printload(ArrayList<Person> loadList) {
        for (Person p: loadList) {
            System.out.println(p.print());
        }
    }

    private static Person fromline(String line) {
        Person fromline = new Person(0, "", 0, 0, 0);
        ArrayList<String> partsOfLine = splitOnSpace(line);
        int age = Integer.parseInt(partsOfLine.get(0));
        int income = Integer.parseInt(partsOfLine.get(2));
        int expenditure = Integer.parseInt(partsOfLine.get(3));
        int willingnesstoInvest = Integer.parseInt(partsOfLine.get(4));
        fromline.updateMaster(age, partsOfLine.get(1), income, expenditure, willingnesstoInvest);
        return fromline;
    }
}
public class FileoperatorTest {

    @Test    public void testload() throws IOException {
        Person d = new Person(20,"David",0,20000,2);
        Person r = new Person(30,"Random guy",50000,30000,2);
        int index = 0;
        ArrayList<Person> check = new ArrayList<> ();
        check.add(d);
        check.add(r);
        load();
        for (Person l: load()) {
            assertEquals(l.getAge(), check.get(index).getAge());
            assertEquals(l.getName(), check.get(index).getName());
            assertEquals(l.getIncome(), check.get(index).getIncome());
            assertEquals(l.getExpenditure(), check.get(index).getExpenditure());
            assertEquals(l.getwillingnesstoInvest(), check.get(index).getwillingnesstoInvest());
            index = index + 1;
        }
    }

    @Test    public void testsave() throws IOException {
        Person test = new Person(30,"test subject",50000,30000,2);
        ArrayList<Person> check = new ArrayList<> ();
        check.add(test);
        Fileoperator fo = new Fileoperator();
        fo.saveListSetter(check);
        fo.save();
        assertEquals(load().get(3).getAge(), check.get(1).getAge());
        assertEquals(load().get(3).getName(), check.get(1).getName());
        assertEquals(load().get(3).getIncome(), check.get(1).getIncome());
        assertEquals(load().get(3).getExpenditure(), check.get(1).getExpenditure());
        assertEquals(load().get(3).getAge(), check.get(1).getAge());
    }
}

  • 1

Reply