且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java中Jar方法的单元测试

更新时间:2023-12-04 08:42:04

测试结果,而不是实现.

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).