简体   繁体   English

使用 Junit4 比较 String[]

[英]Comparing String[] using Junit4

I am trying to perform test on method:我正在尝试对方法进行测试:

public  String[] getFooBar(int size) {
    String[] stringsArray = new String[100];
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i <= size; i++) {
        stringBuilder.append(String.format(i + " "));
        if (i % 3 == 0) {
            stringBuilder.append(String.format("Foo"));
        }
        if (i % 5 == 0) {
            stringBuilder.append(String.format("Bar"));
        }
        stringBuilder.append(System.lineSeparator());
        stringsArray[i] = stringBuilder.toString();
    }
    return stringsArray;
}

} }

I've tried to use index on Array, convert it to Strings using Array.ToString and compare it with String value eg.我尝试在 Array 上使用索引,使用 Array.ToString 将其转换为 Strings 并将其与 String 值进行比较,例如。 "FooBar". “FooBar”。 What is happening when I try to look for one index eg.当我尝试查找一个索引时发生了什么,例如。 50 I get all index from table starting with 0 - 50. I've tried to do ArrayList but result was the same. 50 我从表中获取所有索引,从 0 - 50 开始。我试过做 ArrayList 但结果是一样的。 Anyone guide me how to test this method ?有人指导我如何测试这种方法吗?

The reason for your issue is that you are constantly appending the StringBuilder and then setting each index to that appended value.您的问题的原因是您不断附加 StringBuilder,然后将每个索引设置为该附加值。 That means that each index will look like this:这意味着每个索引将如下所示:

index 0: 0
index 1: 0 1
index 2: 0 1 2
index 3: 0 1 2 3 foo
index 4: 0 1 2 3 foo 4
index 5: 0 1 2 3 foo 4 5 bar

I assume this is the same concept as FizzBuzz, which means you more than likely have another issue: you are appending the index each time regardless of if it's a multiple of 3 or 5. I'm pretty sure you don't want that.我认为这与 FizzBu​​zz 的概念相同,这意味着您很可能有另一个问题:您每次都附加索引,无论它是 3 还是 5 的倍数。我很确定您不想要那样。 You probably want something closer to this:你可能想要更接近这个的东西:

public  String[] getFooBar(int size) {
    String[] stringsArray = new String[100];

    for (int i = 0; i <= size; i++) {
        StringBuilder stringBuilder = new StringBuilder();
        if (i % 3 == 0) {
            stringBuilder.append(String.format("Foo"));
        }
        if (i % 5 == 0) {
            stringBuilder.append(String.format("Bar"));
        }
        if(i % 5 != 0 && i % 3 != 0) {
            stringsArray[i] = Integer.toString(i);
        } else {
            stringsArray[i] = stringBuilder.toString();
        }
    }
    return stringsArray;
}

But there really isn't need for all of that.但真的不需要所有这些。 You can just do this:你可以这样做:

public String[] getFooBar(int size) {
  String[] arr = new String[100];
  for(int i = 0; i <= size; i++) {
    String str = ""; // In situations like this the JVM turns this into a StringBuilder behind the scenes
    if(i % 3 == 0) {
      str += "Foo";
    }
    if(i % 5 == 0) {
      str += "Bar";
    }
    if(i % 3 != 0 && i % 5 != 0) {
      arr[i] = Integer.toString(i);
    } else {
      arr[i] = str;
    }
  }
  return arr;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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