I am trying to use Mockito and PowerMockito to write a Test case for the first time, but I am not able to run it successfully.
I am having trouble mocking a method “delete” present in class HbaseDaoImpl, which is available as an Instance variable in class HbaseIngestionBase, and Class HbaseIngestionBase is being extended by class DeleteDataFromHBase.
public class DeleteDataFromHBase extends HbaseIngestionBase {
protected void map(String arg1, String arg2) {
//some logic performed
//this method I want to mock.
hbaseDao.delete(arg1, arg1.getBytes(), arg2.getBytes());
}
}
public class HbaseIngestionBase {
protected HbaseDaoImpl hbaseDao = null;
protected void setup() {
hbaseDao = initHbaseDao();
}
private void initHbaseDao() {
hbaseDao = new HBaseDaoImpl("some_string", 123);
}
}
public class HbaseDaoImpl implements HbaseDao{
//Constructor
public HbaseDaoImpl(String arg1, int arg2) {
//some logic involving HBase connection, which I want to avoid.
}
// some other methods
public void delete(String arg1, byte[] arg2, byte[] arg3) {
//perform delete operation
}
}
public interface HbaseDao{
void delete(String arg1, byte[] arg2, byte[] arg3);
}
Test Class
@RunWith(PowerMockRunner.class)
@PrepareForTest({DeleteDataFromHBase.class})
public class DeleteDataFromHBaseTest{
@Before
public void setUp{
MockitoAnnoations.initMocks(this);
}
@Spy
DeleteDataFromHBase deleteDataFromHBase = new DeleteDataFromHBase()
@Test
public void testDeletion{
HbaseDao hbaseDao = Mockito.mock(HbaseDao.class);
PowerMockito.doNothing().when(hbaseDao).delete(ArgumentsMatchers.anyString(), ArgumentsMatchers.anyString().getBytes(), ArgumentsMatchers.anyString().getBytes());
//when I call this, it should mock the delete method and do nothing
deleteDataFromHBase.map("abc", "bcd");
}
}
The test fails because the methods is not mocked and its actual contents is executed which involves HBase connection, and that is not possible from my local system.
1