如何在Java中为IO文件创建存根以进行单元测试?

时间:2014-12-24 17:18:00

标签: java unit-testing junit

对于测试课程作业,我需要使用JUnit为已编码的系统创建单元测试。我的系统严重依赖于彼此,它还可以从磁盘上的几个文本文件中写入/读取。 我意识到我必须消除所有依赖以成功进行单元测试,我只是不知道如何为文件创建存根。

欢迎任何代码,工具或概念方面的帮助

 import Objs.*;
import java.io.*;
import java.net.URL;
import java.util.Scanner;

/**
 *This class communicates with the users file by writing to it, reading from it, searching, deleting...
 * 
 */
public class users {
     public static File usersFile = new File("usersFile.txt");
  public static PrintWriter writer;
  static Scanner read ;

public static void write(userObj u){

    try {
                String gather = read();
                String newUser = u.toString();
                writer = new PrintWriter(usersFile);
                writer.append(gather).append(newUser).append("\n");
                writer.close();
                System.out.println("The users' file has been updated");

    }
    catch(FileNotFoundException ex){
        System.out.print("file not found");

    }
}

public static String read(){ 
      String f = null;
    try {
                read = new Scanner(usersFile);
                StringBuilder gather = new StringBuilder();
                while(read.hasNext()){

                    gather.append(read.nextLine()).append("\n");
                    }
                f = gather.toString();
    }
    catch(FileNotFoundException ex){
        System.out.print("file not found");

    }
    return f;
}

public static userObj search(String s){

    userObj foundUser = null;
    try {
                read = new Scanner(usersFile);
                String st=null;
                while(read.hasNext()){
                   if (read.next().equalsIgnoreCase(s)){
                      foundUser = new userObj(); 
                    foundUser.name = s;
                    foundUser.setType(read.next().charAt(0));
                    foundUser.credit  = read.nextDouble();
                   }
                       }
    }
    catch(FileNotFoundException ex){
        System.out.print("file not found");

    }
    return foundUser;
}

public static void remove(userObj u){

    String s = u.name;
    if (search(s) == null){
        return;}

    try {
                read = new Scanner(usersFile);
                StringBuilder gather = new StringBuilder();
                while(read.hasNext()){
                    String info = read.nextLine();
                   if (info.startsWith(s)){
                      continue;
                   }
                   gather.append(info).append("\n");
                       }

                writer = new PrintWriter(usersFile);
                writer.append(gather).append("\n");
                writer.close();
                System.out.println("The user has been deleted");



    }

    catch(FileNotFoundException ex){
        System.out.print("file not found");

    }}

public static void update(userObj u){
    remove(u);
    write(u);
}
}

1 个答案:

答案 0 :(得分:2)

您不需要创建“文件存根”,您需要创建“存根以便从InputStream中读取”。

对于readsearchremove,您使用的是Scanner,它接受​​InputStream作为其重载构造函数之一。如果您添加InputStream参数,则可以使用该参数构建Scanner。正常使用后,您可以传递FileInputStream,同时使用StringBufferInputStream进行测试。

对于writeremove,您使用的是PrintWriter,它接受​​OutputStream作为其重载构造函数之一。如果您添加OutputStream参数,则可以使用该参数构建PrintWriter。正常使用后,您可以传递FileOutputStream,同时使用ByteArrayOutputStream进行测试。如果要将结果作为测试中的字符串读取,请使用toString(String charsetName)

public class Users {
    ...

    public static void write(UserObj u, InputStream input, OutputStream output) {
        ...
        String gather = read(input);
        ...
        writer = new PrintWriter(output);
        ...
    }

    public static String read(InputStream input) {
        ...
        read = new Scanner(input);
        ...
    }

    public static UserObj search(String s, InputStream input) {
       ...
       read = new Scanner(input);
       ...
    }

    public static void remove(UserObj u, InputStream input, OutputStream output) {
       ...
       read = new Scanner(input);
       ...
       writer = new PrintWriter(output);
       ...
    }

    public static void update(UserObj u, InputStream input, OutputStream output) {
        remove(u, input, output);
        write(u, input, output);
    }
}

// Client code example
FileInputStream input = new FileInputStream("usersFile.txt");
FileOutputStream output = new FileOutputStream("usersFile.txt");
...
Users.write(myUser, input, output);
...
String result = Users.read(input);
...
myUser = Users.search(myString, input);
...
Users.remove(myUser, input, output);
...
Users.update(myUser, input, output);

// Testing code example
StringBufferInputStream input = new StringBufferInputStream("...");
ByteArrayOutputStream output = new ByteArrayOutputStream();
...
Users.write(myUser, input, output);
...
String result = Users.read(input);
...
myUser = Users.search(myString, input);
...
Users.remove(myUser, input, output);
...
Users.update(myUser, input, output);
...
result = output.toString("UTF-8"); // see docs for other legal charset names