Java 如何为静态方法编写测试?
在Java 如何为静态方法编写测试?,java,unit-testing,static-methods,Java,Unit Testing,Static Methods,在Fileoperator类中,我尝试了几种方法来启用保存(从屏幕输入到文本文件)和加载(从完全相同的文本文件并在屏幕上打印出来)。因为它们都是静态方法,所以我不能在我的FileoperatorTest中直接引用它们。我如何为他们编写测试 (我已经为Person编写了一些测试) 公共类Fileoperator{ 私有ArrayList存储列表; 私人扫描仪; 智力年龄; 字符串名; 国际贸易收入; 国际收支; 国际投资意愿; 字符串退出; 公共文件操作员(){ saveList=newarray
Fileoperator
类中,我尝试了几种方法来启用保存(从屏幕输入到文本文件)和加载(从完全相同的文本文件并在屏幕上打印出来)。因为它们都是静态方法,所以我不能在我的FileoperatorTest
中直接引用它们。我如何为他们编写测试
(我已经为Person
编写了一些测试)
公共类Fileoperator{
私有ArrayList存储列表;
私人扫描仪;
智力年龄;
字符串名;
国际贸易收入;
国际收支;
国际投资意愿;
字符串退出;
公共文件操作员(){
saveList=newarraylist();
扫描仪=新扫描仪(System.in);
processOperations();
}
私有void processOperations(){
while(true){
Person personEntry=getPerson();
如果(退出等于(“是”)){
personetry.updateMaster(年龄、姓名、收入、支出、投资意愿);
personResult(personetry);
打破
}
personetry.updateMaster(年龄、姓名、收入、支出、投资意愿);
personResult(personetry);
System.out.println(personEntry.print());
}
for(个人p:存储列表){
System.out.println(p.print());
}
}
私人{
Person personEntry=新的Person(0,”,0,0,0);
System.out.println(“年龄:”);
年龄=scanner.nextInt();
scanner.nextLine();
System.out.println(“名称:”);
name=scanner.nextLine();
系统输出打印项次(“收入:”);
收入=scanner.nextInt();
系统输出打印项次(“支出:”);
支出=scanner.nextInt();
System.out.println(“投资意愿:”);
willingnesstoInvest=scanner.nextInt();
scanner.nextLine();
System.out.println(“完成”(是或否)”;
exit=scanner.nextLine();
回归人格;
}
私人无效人员结果(人员p){
保存列表。添加(p);
}
公共静态void mainheloper(){
试一试{
printload(load());
}捕获(IOE异常){
e、 printStackTrace();
}
Fileoperator fo=新的Fileoperator();
试一试{
fo.save();
}捕获(IOE异常){
e、 printStackTrace();
}
}
public void save()引发IOException{
列表行=Files.readAllLines(path.get(“output.txt”);;
PrintWriter=新的PrintWriter(“output.txt”、“UTF-8”);
for(个人p:存储列表){
添加(p.getAge()+”,“+p.getName()+”,”
+p.getIncome()+“,+p.GetExpense()+”,“+p.getwillingnesstoInvest());
}
用于(字符串行:行){
ArrayList PartsOffline=拆分空间(行);
System.out.println(“年龄:+partsOfLine.get(0)+”,”;
System.out.println(“名称:“+partsOfLine.get(1)+”,”;
System.out.println(“收入”+部分行.get(2)+“,”;
System.out.println(“支出”+partsOfLine.get(3)+“,”;
System.out.println(“WillingnesstoInvest”+partsOfLine.get(4)+“,”;
writer.println(行);
}
writer.close();//注意——如果您错过了这一点,文件将不会被写入。
}
公共静态ArrayList splitOnSpace(字符串行){
String[]splits=line.split(“,”);
返回新的ArrayList(Arrays.asList(splits));
}
公共静态ArrayList load()引发IOException{
列表行=Files.readAllLines(path.get(“output.txt”);
ArrayList loadList=新建ArrayList();
用于(字符串行:行){
if(第行等于(“”){
打破
}否则{
Person fromline=fromline(line);
loadList.add(fromline);
}
}
返回装载清单;
}
公共静态无效打印加载(ArrayList加载列表){
用于(人员p:装载列表){
System.out.println(p.print());
}
}
private static Person fromline(字符串行){
Person fromline=新的Person(0,“,0,0,0);
ArrayList PartsOffline=拆分空间(行);
int age=Integer.parseInt(partsOfLine.get(0));
int income=Integer.parseInt(partsOfLine.get(2));
int expensation=Integer.parseInt(partsOfLine.get(3));
int-willingnesstoInvest=Integer.parseInt(partsOfLine.get(4));
fromline.updateMaster(年龄、PartsOffLine.get(1)、收入、支出、投资意愿);
从生产线返回;
}
}
将其设置为静态的目的是什么?您好,欢迎使用StackOverflow!您需要包括FileoperatorTest
看起来像什么:)。(可能您只需要在方法调用前面加上类名,即,Fileoperator.myMethod(…)
),因为它们都是静态方法请原谅我的无知,但您指的是哪些方法?它们的名字是什么?我想在main方法中调用mainheloper方法(它在main类中),否则我不能这样做。save()和load(),实际上我在尝试为它们编写测试时也不知道如何处理IOException。使它们成为静态的目的是什么?您好,欢迎使用StackOverflow!您需要包括FileoperatorTest
看起来像什么:)。(可能您只需要在方法调用前面加上类名,即,Fileoperator.myMethod(…)
),因为它们都是静态方法请原谅我的无知,但您指的是哪些方法?它们的名字是什么?我想在main方法中调用main helper方法(它在main类中),否则我不能这样做。save()和load(),实际上我在尝试为它们编写测试时也不知道如何处理IOException。
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();
}
private void processOperations() {
while (true) {
Person personEntry = getPerson();
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 getPerson() {
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;
}
}