天天看點

Java單元測試之 Apache HBase

HBase的安裝和部署在這裡就不贅述了,一般情況下是不需要對HBase進行mock測試,由于官方對此沒有做過多介紹,是以和大家分享一下。

Maven依賴

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-server</artifactId>
    <version>${hbase.version}</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-shaded-testing-util</artifactId>
    <version>${hbase.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-zookeeper</artifactId>
    <version>${hbase.version}</version>
    <scope>provided</scope>
    <type>test-jar</type>
</dependency>
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-common</artifactId>
    <version>${hbase.version}</version>
    <scope>provided</scope>
    <type>test-jar</type>
</dependency>
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-testing-util</artifactId>
    <version>${hbase.version}</version>
    <scope>test</scope>
</dependency>           

Mock測試示例

public class HBaseMockTest {

  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
  private static final String NAMESPACE = "TEST";
  private static final TableName TABLE_NAME =
      TableName.valueOf(TestMetaRepair.class.getSimpleName());
  private static final byte[] family = Bytes.toBytes("test");
  private Table table;

  @BeforeClass
  public static void beforeClass() throws Exception {
    TEST_UTIL.startMiniCluster(3);
  }

  @AfterClass
  public static void afterClass() throws Exception {
    TEST_UTIL.shutdownMiniCluster();
  }

  @Before
  public void setup() throws Exception {
    table = TEST_UTIL.createMultiRegionTable(TABLE_NAME, family, 5);
    TEST_UTIL.waitUntilAllRegionsAssigned(TABLE_NAME);
  }

  @After
  public void tearDown() throws Exception {
    TEST_UTIL.deleteTable(TABLE_NAME);
  }

  @Test
  public void test() throws Exception {

    // table.put(...);
  }

}