java中Jar方法的单元测试 [英] Unit testing of Jar methods in java

查看:34
本文介绍了java中Jar方法的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apache POI 方法在 Java 中写入 Excel 工作表.我必须对我编写的方法进行单元测试.我用过很多 Apache POI 的方法.我应该存根 Apache POI 不同对象类的所有方法吗?

I'm using Apache POI methods for writing to excel sheets in java. I have to do unit-testing for the methods which I have written. I have used many methods of Apache POI. Should I stub all the methods of the different object classes of Apache POI?

示例:我创建了用于写入使用 Apache POI 方法的单元格的包装方法.

Example: I have created wrapper methods for writing to cell which are using Apache POI methods.

protected void writeCell(int rowNum, int colNum, String value, Sheet sheet)
{
    if(value == null)
    {
        return;
    }
    Row row = sheet.getRow(rowNum);
    Cell cell = row.createCell(colNum);
    cell.setCellValue();
  }

} 

我是否应该模拟诸如 Sheet.classgetRow 和 Row 类的 createCell 之类的方法?

Should I mock even the methods like getRowof the Sheet.class and createCell of the Row class?

推荐答案

测试结果,而不是实现.

Test for results, not implementation.

即如果我写一个单元格,我现在可以从那个单元格读回那个值吗?

I.e. If I write a cell, can I now read that value back from that cell?

你还向我们展示了一个 protected 方法,你应该只需要测试你的类的公共接口.如果您无法通过公共界面看到结果,则您的课程可能做得太多(单一职责原则).

Also you showed us a protected method, you should only need to test the public interface of your classes. If you can't see results though the public interface, your class is probably doing too much (Single Responsibility Principle).

然而,其他考虑因素是速度和脆弱性,因为 Apache POI 正在读取和写入实际文件,编写这些测试会有点困难,而且它们会更慢.几个测试没问题,但如果整个套件都在读写文件,那么它会变慢,并且整个测试套件最好在几秒钟内运行.

However, the other considerations are ones of speed and fragility, because Apache POI is reading and writing to actual files, it will be a little harder to write these tests and they will be slower. OK for a few tests, but if the whole suite is reading and writing files then it's going to get slow, and the entire test suite should run in a matter of a few seconds ideally.

所以我会创建一个接口来封装一个excel表和你想用它做什么,这可能是:

So I would create an interface that encapsulates an excel sheet and what you want to do with it, this might be:

public interface StringGrid {
    String readCell(int rowNum, int colNum);
    void writeCell(int rowNum, int colNum, String value);
}

现在我可能会在没有自动化测试的情况下快速实现,或者只是围绕 Apache POI 进行一些简单的测试,但是我的套件的其余部分将针对 StringGrid 实现,我可以继续创建许多快速运行围绕我的代码进行测试.

Now I might do a quick implementation without automated tests, or just a few simple tests around Apache POI, but then the rest of my suite would test against a Fake implementation of the StringGrid and I can get on creating lots of fast running tests around my code.

所以这些可能看起来像:

So these might look like:

真正的实现,仅用于其测试和实时程序.

The real implementation, used only in its tests and the live program.

public final class ApacheSheetStringGrid implements StringGrid {

    private final Sheet theApacheSheet;

    public ApacheSheetStringGrid(Sheet theApacheSheet) {
        this.theApacheSheet = theApacheSheet;
    }

    public String readCell(int rowNum, int colNum){
       ...
    }

    public void writeCell(int rowNum, int colNum, String value) {
      Row row = theApacheSheet.getRow(rowNum);
      Cell cell = row.createCell(colNum);
      cell.setCellValue();
    }
}

The Fake(StringGrid 的一个有效的、快速的、仅在内存中的实现),用于所有其他测试:

The Fake (a working, fast, in-memory only implementation of StringGrid), for all other testing:

public final class FakeStringGrid implements StringGrid {

    private final Map<String, String> contents = new HashMap<String, String>();

    private static String getKey(int rowNum, int colNum) {
       return rowNum + ", " + colNum;
    }

    public String readCell(int rowNum, int colNum){
       return contents.get(getKey(rowNum, colNum));
    }

    public void writeCell(int rowNum, int colNum, String value) {
       contents.put(getKey(rowNum, colNum), value);
    }
}

这有额外的好处,您可以稍后将实时实现换成另一个,也许使用其他 POI 方法之一,甚至是谷歌表实现,您只需要添加一些测试和新实现就可以了'不需要修改任何代码(开闭原则).

This has extra benefits, you can later swap out the live implementation for another, maybe using one of the other POI approaches or even google sheets implementation and you'll only need to add a few tests and a new implementation and won't need to modify any code (Open Closed Principle).

这篇关于java中Jar方法的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆