2

如何使用 Junit 对该代码进行单元测试

public class Diamond {

   public void DiamondShape(int num) {

       for(int ucount=num;ucount>0;ucount--) {
    //Loop to print blank space
        for(int count_sp=1;count_sp<ucount;count_sp++)
            System.out.printf(" ");
    //Loop to print *
        for(int count_ast=num;count_ast>=ucount;count_ast--)
            System.out.printf("* ");
        System.out.println();
       }
//Loop for lower half
  for(int lcount=1;lcount<num;lcount++) {
    //Loop to print blank space
    for(int count_sp=0;count_sp<lcount;count_sp++)
            System.out.printf(" ");
    //Loop to print *
        for(int count_ast=num-1;count_ast>=lcount;count_ast--)
            System.out.printf("* ");
    System.out.println();
    }
  } 
}

我是单元测试的新手,想要一些关于单元测试的指导。

num=3 时输出

   *
  * *
 * * *
  * *
   *

这就是输出应该是这样的,num表示中心线上的星号

4

3 回答 3

4

而不是做 sysout 你应该重构并使你的方法返回输出。然后,您可以验证输出。

另一个选项是在您的 junit 测试中创建一个输出流并将其设置在

System.setOut(your output stream);

然后,您可以验证输出流。

但这并不可靠,因为如果您的程序执行一些其他代码也写入 sysout,那么您的输出流也将包含该数据。

于 2013-04-15T08:10:47.150 回答
2

为了测试一种方法,它必须执行以下操作之一:

  • 返回一个可以与期望值进行比较的值
  • 更改传递给它的对象的状态
  • 抛出一个你可以捕获的异常
  • 更改对象的某些状态,您可以通过调用另一个方法来验证这些状态

因此,通常应避免返回 void 并将其结果写入 System.out 的方法。

要解决此问题,您可以执行以下操作之一:

  • 返回一个字符串而不是写入标准输出。
  • 使该方法采用PrintStream对象并写入该对象。System.out 是一个 PrintStream 对象,因此您可以在生产代码中传递它。但是,在您的测试代码中,您可以传递自己的 PrintStream 对象,该对象不写入标准输出,而是允许检查写入的内容。这种技术称为依赖注入
  • 使该方法写入私有 PrintStream 或 String,并向该类添加两个新方法。一种是获取该变量的内容,以便您可以测试它是否正确,另一种是将该变量写入 System.out。
于 2013-04-15T08:24:28.920 回答
0

而不是直接打印您的形状,您可以将形状存储在ArrayList<String>

public class Diamond {
    private List<String> shape = null;

    public void createShape(int num) {
        shape = new ArrayList<String>();
        String shapeLine = "";
        for(int ucount=num;ucount>0;ucount--) {
            for(int count_sp=1;count_sp<ucount;count_sp++) {
                shapeLine += " ";
            }
            for(int count_ast=num;count_ast>=ucount;count_ast--) {
                shapeLine += "* ";
            }
            shape.add(shapeLine);
        }

        shapeLine = "";
        for(int lcount=1;lcount<num;lcount++) {
           for(int count_sp=0;count_sp<lcount;count_sp++) {
                shapeLine += " ";
           }
           for(int count_ast=num-1;count_ast>=lcount;count_ast--) {
               shapeLine += "* ";
           }
           shape.Add(shapeLine);
        }
    } 

    public void printShape(OutStream out) {
        if(shape != null) {
            for(String shapeLine : shape) {
                out.println(shapeLine);
            }
        }
    }

    public List<String> getShape() {
        return shape;
    } 
}

现在您可以测试您的代码:

@Test
public void testShape() {
    Diamond diamond = new Diamond();
    diamond.createShape(3);
    List<String> shape = diamond.getShape();

    assertNotNull(shape);
    assertEquals(5, shape.size());
    assertEquals("  * ", shape.get(0));
    assertEquals(" * * ", shape.get(1));
    assertEquals("* * * ", shape.get(2));
    assertEquals(" * * ", shape.get(3));
    assertEquals("  * ", shape.get(4));
}

打印你的形状只需调用

diamond.printShape(System.out);

上半部分和下半部分循环的内部部分完全相同,可以重构为自己的方法。

于 2013-04-15T08:31:39.930 回答