在java的罐子方法单位测试 [英] Unit testing of Jar methods in java

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

问题描述

我使用写入Excel表格在Java的Apache POI方法。我不得不为我写的方法做单元测试。我使用的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();
  }

} 

我应该嘲笑甚至像的getRow 的Sheet.class createCell的方法 Row类的?

推荐答案

有关结果测试,而不是执行。

Test for results, not implementation.

即。如果我写一个单元,我现在可以读取该值从电池背?

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

您也向我们展示了一个保护方法,你应该只需要测试你的类的公有接口。如果你不能看到的结果,虽然公共接口,你的类可能做得太多(单一职责原则)。

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 ,我可以得到创造大量的的跑来跑去测试的我的的code。

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.

所以这可能是这样的:

真正实现,只有在它的测试和现场直播节目中。

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();
    }
}

该假(一个工作,速度快,内存只有实施 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的方法之一,甚至Google表格实施和你只需要添加一些测试和一个新的实现,并荣获吨需要修改任何code(开闭原则)。

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的罐子方法单位测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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