title: How to unit test Java Hbase API
body: <p>I am using the Java HBase API to get a value from Hbase. This is my code.</p>
  
  <pre><code>public class GetViewFromHbaseBolt extends BaseBasicBolt {
  
  private HTable table;
  private String zkQuorum;
  private String zkClientPort;
  private String tableName;
  
  public GetViewFromHbaseBolt(String table, String zkQuorum,
          String zkClientPort) {
      this.tableName = table;
      this.zkQuorum = zkQuorum;
      this.zkClientPort = zkClientPort;
  }
  
  @Override
  public void prepare(Map config, TopologyContext context) {
      try {
          table = getHTable();
      } catch (IOException e) {
  
          e.printStackTrace();
      }
  }
  
  @Override
  public void execute(Tuple tuple, BasicOutputCollector collector) {
      try {
          if (tuple.size() &gt; 0) {
  
              Long dtmid = tuple.getLong(0);
  
              byte[] rowKey = HBaseRowKeyDistributor.getDistributedKey(dtmid);
              Get get = new Get(rowKey);
              get.addFamily("a".getBytes());
  
              Result result = table.get(get);
              System.out.println(result);
              byte[] bidUser = result.getValue("a".getBytes(),
                      "co_created_5076".getBytes());
              collector.emit(new Values(dtmid, bidUser));
          }
  
      } catch (IOException e) {
          e.printStackTrace();
  
      }
  
  }
  
  @Override
  public void declareOutputFields(OutputFieldsDeclarer declarer) {
  
      declarer.declare(new Fields("dtmi", "bidUser"));
  
  }
  
  // setting up the conections for Hbase
  protected HTable getHTable() throws IOException {
      HTable table = null;
      Configuration conf;
      conf = HBaseConfiguration.create();
      conf.set("hbase.zookeeper.property.clientPort", zkClientPort);
      conf.set("hbase.zookeeper.quorum", zkQuorum);
  
      table = new HTable(conf, tableName);
  
      return table;
  }
  </code></pre>
  
  <p>This works fine. Now I am writing a Unit Test using the Mockito API. On my test class I am getting a java.langNullPointerException when this  when(table.get(any(Get.class))).thenReturn(result); is called.</p>
  
  <pre><code>   public class GetViewFromHbaseBoltTest {
  
  @Mock
  private TopologyContext topologyContext;
  @Mock
  private  HTable table;
  //HTable table = mock(HTable.class);
  
  
  @Test
  public void testExecute() throws IOException {
  
      long dtmId = 350000000770902930L;
      final byte[] COL_FAMILY = "a".getBytes();
      final byte[] COL_QUALIFIER = "co_created_5076".getBytes();
  
      // A mock tuple with a single dtmid
      Tuple tuple = mock(Tuple.class);
      when(tuple.size()).thenReturn(1);
      when(tuple.getLong(0)).thenReturn(dtmId);
  
      List&lt;KeyValue&gt; kvs = new ArrayList&lt;KeyValue&gt;();
      kvs.add(new KeyValue(COL_FAMILY, COL_QUALIFIER, Bytes
              .toBytes("ExpedtedBytes")));
      Result result = new Result(kvs);
  
  
      when(table.get(any(Get.class))).thenReturn(result);
  
      BasicOutputCollector collector = mock(BasicOutputCollector.class);
  
      GetViewFromHbaseBolt bolt = mock(GetViewFromHbaseBolt.class);
      // Execute the bolt.
      bolt.execute(tuple, collector);
  
      ArgumentCaptor&lt;Values&gt; valuesArg = ArgumentCaptor
              .forClass(Values.class);
      verify(collector).emit(valuesArg.capture());
      ArrayList&lt;Object&gt; d = valuesArg.getValue();
  
  // verify
  
  }
  </code></pre>