Mocking method returns null

I have the following method

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

For this, I wrote the following junit test code

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

when testing a method

statement

Table table = getTableInstance("Sampletable");

works fine and gives a mocked table object, but the statement

table.getScanner(scan);

returns null.

Please find the source code of the table interface in the link below

http://grepcode.com/file/repo1.maven.org/maven2/org.apache.hbase/hbase-client/1.1.1/org/apache/hadoop/hbase/client/Table.java

Please help me with this ??

+4
source share
1 answer

Replace the last line of the test case with

 PowerMockito.when(mocktable.getScanner(Mockito.any(Scan.class))).thenReturn(mockrs);

He should work

+1
source

Source: https://habr.com/ru/post/1663844/


All Articles