gpt4 book ai didi

java - 如何为 IO 文件创建 stub 以在 Java 中进行单元测试?

转载 作者:行者123 更新时间:2023-11-30 03:36:59 24 4
gpt4 key购买 nike

对于测试类(class)作业,我需要使用 JUnit 为已编码的系统创建单元测试。我的系统彼此严重依赖,并且它还从磁盘上的几个文本文件写入/读取。我意识到我必须消除所有依赖性才能成功进行单元测试,我只是不知道如何为文件创建 stub 。

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

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

最佳答案

您不需要创建“文件 stub ”,您需要创建“用于从InputStream读取的 stub ”。

对于读取搜索删除,您使用的是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

关于java - 如何为 IO 文件创建 stub 以在 Java 中进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27640278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com