模拟方法返回null



我有以下方法

public ResultScanner getScanner(Scan scan) {
    Table table = getTableInstance("Sampletable");
    return table.getScanner(scan);
}

为此,我已经写了Junit测试代码

Connection mockconnection = PowerMockito.mock(Connection.class);
Table mocktable = PowerMockito.mock(Table.class);
PowerMockito.when(mockconnection.getTable(TableName.valueOf(Mockito.anyString())))
    .thenReturn(mocktable);
Scan mockedScan = PowerMockito.mock(Scan.class);
ResultScanner mockrs = PowerMockito.mock(ResultScanner.class);
PowerMockito.when(mocktable.getScanner(mockedScan)).thenReturn(mockrs);

测试方法

语句

Table table = getTableInstance("Sampletable");

运行良好并给出模拟的表对象,但语句

table.getScanner(scan);

返回null。

请在下面的链接中找到表接口的源代码

http://grepcode.com/file/repo1.maven.org/maven2/maven2/org.apache.hbase.hbase/hbase-client/1.1.1.1/org/apache/hadoop/hadoop/hadoop/hbase/hbase/Client/Client/table/table.java p>请指导我?

替换测试用例的最后一行
 PowerMockito.when(mocktable.getScanner(Mockito.any(Scan.class))).thenReturn(mockrs);

它应该工作

相关内容

  • 没有找到相关文章

最新更新