gpt4 book ai didi

java - 使用 Mockito/PowerMockito 伪造方法参数

转载 作者:行者123 更新时间:2023-11-30 07:27:10 24 4
gpt4 key购买 nike

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**/
}

我必须测试我的类 Service,它使用类 ExcelWriter 的 protected 方法 writeToCell,如下所示:

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

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

ExcelWriter类的设计如下:

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

writeToCell() 方法设计有 protected 访问修饰符:

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

}

我必须对 handleRequest 进行单元测试类的方法ServiceImpl它使用 ExcelWriter 的 writeDetailsForCategory ,它正在使用 protected 方法 writeToCell 。我已经通过了FakeExcelSheetwriteToCell方法。我将对 Map <Object, FakeExcelRow> 的值进行断言在这之后。我该如何传入 protected 中的 fakeExcelSheet 参数writeToCell方法?我的限制是我只能使用 Mockito 或 PowerMockito。

最佳答案

继续我的other answer ,您可以将特定的 ExcelWriter 替换为 StringGridWriter,后者写入给定的 StringGrid。它不知道也不关心它是否是 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)
}
}
}

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

Sheet theApacheSheet = ...;

StringGrid grid = new ApacheSheetStringGrid(theApacheSheet);

StringGridWriter writer = new StringGridWriter(grid);

ServiceImpl service = new ServiceImpl(writer);

其中 ApacheSheetStringGrid 是根据我的其他答案。所以现在,即使是 ServiceImpl 也不知道这是在使用 excel。这个依赖项的注入(inject)是 Dependency Inversion Principle在实践中。

看看我如何快速摆脱复杂的 Excel 界面,并将其简化为一个抽象,一个功能子集,这只是我的客户想要的界面 ( Interface Segregation Principle ),这使得它既易于测试,并且易于替换为其他实现。通过不抽象,您可以将代码与 apache excel 实现紧密耦合。

关于java - 使用 Mockito/PowerMockito 伪造方法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36667172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com