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

查看:456
本文介绍了使用伪造的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);

}

我有单元测试,它使用ExcelWriter的 ServiceImpl 的handleRequest 方法writeDetailsForCategory ,这是使用受保护的方法 writeToCell 。我已经通过了 FakeExcelSheet writeToCell method.I将断言上的值地图&LT;对象,FakeExcelRow&GT; 在此之后。我应该如何传入fakeExcelSheet参数保护 writeToCell 的方法?我的约束是,我可以使用或的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.

推荐答案

从我的对方的回答,你可以更换特定 ExcelWriter StringGridWriter ,它写入到一个给定的 StringGrid 。它不知道也不关心它是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.

请参阅我如何得到迅速从复杂的远优于接口和熬下来,只是一个抽象的功能的子集,这仅仅是我的客户想要的界面(的接口分离原则的),这使得它既容易测试,并容易与其它实现方式来代替。如果不提取,你是你的code紧密耦合到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天全站免登陆