使用 Mockito/PowerMockito 伪造方法参数 [英] Faking Method parameters using Mockito/PowerMockito

查看:70
本文介绍了使用 Mockito/PowerMockito 伪造方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class FakeExcelSheet implements Sheet
{
     private final Map<Integer, FakeExcelRow> fakeSheet = new HashMap<Integer, FakeExcelRow>();

     @Override
     public Row createRow(int rowNum)
     {
          Row row = new FakeExcelRow(rowNum, fakeSheet);
          fakeSheet.put(rowNum, (FakeExcelRow)row);
          return row;
     }

     @Override
     public Row getRow(int rowNum)
     {
          if (fakeSheet.get(rowNum) != null)
          {
               return fakeSheet.get(rowNum);
          }
          else
          {
               return createRow(rowNum);
          }

     }

     /**Other unimplemented methods of Apache POI class Sheet**/ 
}

public class FakeExcelRow implements Row
{
    private int rowNum;
    private Map<Integer, FakeExcelRow> sheet;
    private List<Object> cellList = new LinkedList<Object>();

    public FakeExcelRow(int rowNum , Map<Integer,FakeExcelRow> sheet)
    {
          this.rowNum = rowNum;
          this.sheet = sheet;
    }

    @Override
    public Cell createCell(int colNum)
    {
         return new FakeExcelCell(colNum, rowNum, sheet);
    }

    /**Other unimplemented methods of Apache POI class Row**/
}

public class FakeExcelCell implements Cell
{
      private int colNum;
      private int rowNum;
      private Map<Integer, FakeExcelRow> sheet;

      public FakeExcelCell(int colNum, int rowNum, Map<Integer, FakeExcelRow> sheet)
      {
          this.colNum = colNum;
          this.rowNum = rowNum;
          this.sheet = sheet;
      }

      public void setCellValue(String value)
      {
           if(sheet != null)
           {
                FakeExcelRow fakeExcelRow = sheet.get(rowNum);
                List<Object> cellList = fakeExcelRow.getCellList();
                cellList.add(value);
           }
      }

      /**Other unimplemented methods of Apache POI class Cell**/
}

我必须像这样测试我的类服务,它使用类 ExcelWriter 的受保护方法 writeToCell :

I have to test the my class Service which is using class ExcelWriter's protected method writeToCell like this :

public class ServiceImpl
{
     ........
     .
     .
     private ExcelWriter excelWriter;
     private Sheet sheet;
     private void handleRequest()
     {

           while("Till there are more rows")
           {
           excelWriter.writeDetailsForCategory(List<String> listOfItems)
           }
     }
 }

ExcelWriter 类是这样设计的:

The ExcelWriter Class is designed like this:

public class ExcelWriter()
{
     protected Sheet sheet;
     public void writeDetailsForCategory(List<String> listOfItems)
     {
           for(String item : listOfItems)
           {
                 writeToCell(rowNum, columnNum, value, sheet);
                 columnNum++;
           }
     }
}

writeToCell() 方法设计有受保护的访问修饰符:

The writeToCell() method is designed with protected access modifier:

protected void writeCell(int rowNum, int colNum, String value, Sheet sheet)
{

    if (value == null)
    {
        return;
    }

    Row row = sheet.getRow(rowNum);

    if (row == null)
    {
        row = sheet.createRow(rowNum);
    }

    Cell cell = row.createCell(colNum);
    cell.setCellValue(value);

}

我必须对 ServiceImpl 类的 handleRequest 方法进行单元测试,该类使用 ExcelWriter 的 writeDetailsForCategory ,它使用受保护的方法 writeToCell.我已经在 writeToCell 方法中传递了 FakeExcelSheet.在此之后,我将对 Map 的值进行断言.我应该如何在 protected writeToCell 方法中传入 fakeExcelSheet 参数?我的限制是我只能使用 Mockito 或 PowerMockito.

I have to unit test the handleRequest method of the class ServiceImpl which uses ExcelWriter's writeDetailsForCategory , which is using protected method writeToCell. I have pass the FakeExcelSheet in the writeToCell method.I will assert on the values of the Map <Object, FakeExcelRow> after this. How should I pass in the fakeExcelSheet parameter in protected writeToCell method? My constraints are that I can use Mockito or PowerMockito only.

推荐答案

继续我的其他答案,你可以用写入给定 StringGridStringGridWriter 替换特定的 ExcelWriter.它不知道或不在乎它是 excel.

Continuing on from my other answer, you could replace the specific ExcelWriter with a StringGridWriter, which writes to a given StringGrid. It does not know or care that it is excel.

public final class StringGridWriter() {
     private final StringGrid grid;

     public StringGridWriter(StringGrid grid) {
        this.grid = grid;
     }

     public void writeDetailsForCategory(List<String> listOfItems) {
           for(String item : listOfItems) {
                 grid.writeToCell(rowNum, columnNum, value);
                 columnNum++;
           }
     }
}

服务中的使用:

public class ServiceImpl {
     ........
     .
     .
     private final StringGridWriter gridWriter;

     public ServiceImpl(StringGridWriter gridWriter) {
         this.gridWriter = gridWriter;
     }

     private void handleRequest() {    
           while("Till there are more rows"){
             gridWriter.writeDetailsForCategory(List<String> listOfItems)
           }
     }
 }

这就是您分配给它使用它的方式:

And this is how you'd assign to it to use it:

Sheet theApacheSheet = ...;

StringGrid grid = new ApacheSheetStringGrid(theApacheSheet);

StringGridWriter writer = new StringGridWriter(grid);

ServiceImpl service = new ServiceImpl(writer);

其中 ApacheSheetStringGrid 与我的其他答案相同.所以现在,即使 ServiceImpl 也不知道这是使用 excel.这种依赖注入是实践中的依赖倒置原则.

Where ApacheSheetStringGrid is as per my other answer. So now, even the ServiceImpl has no idea that this is using excel. This injection of dependencies is the Dependency Inversion Principle in practice.

看看我如何快速摆脱复杂的 excel 界面并将其归结为一个抽象,一个功能子集,这只是我的客户想要的界面 (接口隔离原则),这使得它既易于测试,又易于替换为其他实现.通过不抽象,您将代码与 apache excel 实现紧密耦合.

See how I rapidly get away from the complicated excel interfaces and boil it down to just an abstraction, a subset of functionality, which is only the interface my client wants (Interface Segregation Principle), which makes it both easy to test, and easy to replace with other implementations. By not abstracting you are tightly coupling your code to the apache excel implementation.

这篇关于使用 Mockito/PowerMockito 伪造方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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