简体   繁体   中英

How do I create stubs for IO files to unit test in Java?

For a testing course assignment, I need to create unit tests for my already-coded system using JUnit. My system is heavily dependent on each other and it also writes/reads from a couple of text files on my disk. I realize I have to eliminate all dependancies to successfully unit test, I just don't know how to create stubs for Files.

Any help in code, tools or concepts is welcome

 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);
}
}

You don't need to create "stubs for files", you need to create "stub for reading from an InputStream".

For read , search , and remove you're using Scanner , which accepts an InputStream as one of its overloaded constructors. If you add an InputStream parameter, you can use that to construct your Scanner . With normal use, you can pass a FileInputStream , while using a StringBufferInputStream for testing.

For write and remove you're using a PrintWriter , which accepts an OutputStream as one of its overloaded constructors. If you add an OutputStream parameter, you can use that to construct your PrintWriter . With normal use, you can pass a FileOutputStream , while using a ByteArrayOutputStream for testing. If you want to read the result as a string from your test, use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM